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

Unable to write list data to csv file | Python |

我在 Google Collab 中编写了代码,但是在将数据写入 csv 文件后,它无法打开。

没有找到我要去的地方,所有代码看起来都很完美。

我坚信问题的发生是因为最后一条记录

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

错误:

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 数据:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
    <food>
        <name>Belgian Waffles</name>
        <price>.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>.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>.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>.50</price>
        <desc>Thick slices made from our homemade sourdough bread</desc>
        <calories>600</calories>
    </food>
    <food>
        <name>Homestyle Breakfast</name>
        <price>.95</price>
        <desc>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</desc>
        <calories>950</calories>
    </food>
</breakfast_menu>

代码:

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(',')))

列表中的输出:

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

上面的列表数据需要写入csv文件是不是有点挑战?

非常感谢任何解决方案!!!

我是这样解决的:

out2 = [[i] for i in out]

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

工作证明: