简体   繁体   English

无法将列表数据写入 csv 文件 | Python |

[英]Unable to write list data to csv file | Python |

I have written a code in Google Collab but after writing the data to csv file it does not get open.我在Google Collab 中编写了代码,但在将数据写入 csv 文件后,它无法打开。

Not getting where I am going wrong all code looks perfect.没有找到我要去的地方,所有代码看起来都很完美。

I strongly believe the issue is getting occurred because of last record我坚信这个问题是因为最后一条记录而发生的

'$6.95,Two eggs, bacon or sausage, toast, and our ever-popular hash browns,Homestyle Breakfast,950'

Error:错误:

文件无法打开

Row 0 given with size different than 91 (the number of columns in the table).
Error: Row 0 given with size different than 91 (the number of columns in the table).
    at k.insertRows (https://colab.research.google.com/v2/external/js/modules/gviz_loader.js:182:61)
    at k.addRows (https://colab.research.google.com/v2/external/js/modules/gviz_loader.js:183:76)
    at k.addRow (https://colab.research.google.com/v2/external/js/modules/gviz_loader.js:183:236)
    at k.addRow (https://colab.research.google.com/v2/external/js/modules/gviz_loader.js:500:59)
    at Q9.fillTable (https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20220602-060045-RC00_452505088:5499:1243)
    at va.program_ (https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20220602-060045-RC00_452505088:5499:847)
    at xa (https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20220602-060045-RC00_452505088:20:336)
    at va.next_ (https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20220602-060045-RC00_452505088:18:474)
    at ya.next (https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20220602-060045-RC00_452505088:21:206)
    at b (https://colab.research.google.com/v2/external/external_polymer_binary.js?vrz=colab-20220602-060045-RC00_452505088:21:468)

xml data: xml 数据:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>$5.95</price>
        <desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
        <calories>650</calories>
    </food>
    <food>
        <name>Strawberry Belgian Waffles</name>
        <price>$7.95</price>
        <desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
        <calories>900</calories>
    </food>
    <food>
        <name>Berry-Berry Belgian Waffles</name>
        <price>$8.95</price>
        <desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
        <calories>900</calories>
    </food>
    <food>
        <name>French Toast</name>
        <price>$4.50</price>
        <desc>Thick slices made from our homemade sourdough bread</desc>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>$6.95</price>
        <desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
        <calories>950</calories>
    </food>
</breakfast_menu>

Code:代码:

import xml.etree.ElementTree as ET
import csv

readxml = ET.parse('/content/sample_data/food_details.xml')
get_root_element = readxml.getroot()
get_headers = set([ elem.tag for child in get_root_element for elem in child ])
out = []

for fd in get_root_element:
  temp = []
  for header in get_headers:
    result = fd.find(header)
    if result is not None:
      if result.text is not None:
        temp.append(result.text)
      else:
        temp.append('null')
    else:
      temp.append('null')
  out.append('|'.join(temp))


with open('/content/sample_data/xyz.csv','w') as wr:
  csv_wr = csv.writer(wr)

  for i in out:
    csv_wr.writerow(list(i.strip(',')))

Output in List:列表中的 Output:

['$5.95,Two of our famous Belgian Waffles with plenty of real maple syrup,Belgian Waffles,650',
 '$7.95,Light Belgian waffles covered with strawberries and whipped cream,Strawberry Belgian Waffles,900',
 '$8.95,Light Belgian waffles covered with an assortment of fresh berries and whipped cream,Berry-Berry Belgian Waffles,900',
 '$4.50,Thick slices made from our homemade sourdough bread,French Toast,600',
 '$6.95,Two eggs, bacon or sausage, toast, and our ever-popular hash browns,Homestyle Breakfast,950']

The above list data need to be written to csv file is bit challenging?上面的列表数据需要写到csv文件是不是有点难度?

Any solution is much appreciated !!!非常感谢任何解决方案!

I solved it like this:我这样解决了:

out2 = [[i] for i in out]

with open('test.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(out2)

Proof of work:工作证明:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM