Home > Back-end >  Unable to write list data to csv file | Python |
Unable to write list data to csv file | Python |

Time:06-05

I have written a code in Google Collab but after writing the data to csv file it does not get open.

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 :

File not able to open

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

['$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 ?

Any solution is much appreciated !!!

CodePudding user response:

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:

enter image description here

  • Related