简体   繁体   中英

Writing to csv in python — delimiters

I am trying to write in csv file the result of my code but somehow it writes incorrectly.

My code is:

import xml.etree.ElementTree as ET
import csv

with open('myfile.xml', 'rt') as f:
tree = ET.parse(f)

for sentence in tree.iter('sentence'):
    certainty = sentence.attrib.get('certainty')
    ccue = sentence.find('ccue')
    with open('new_file.csv', 'w', newline='') as csvfile:
       writer = csv.writer(csvfile, delimiter='|',
                            quotechar='^', quoting=csv.QUOTE_MINIMAL)
       if certainty and (ccue is not None):
           writer.writerow('  %s | %s | %s' % (certainty, ''.join(sentence.itertext()), ccue.text))
       else:
           writer.writerow('  %s | | %s' % (certainty,sentence.text))

So I want to get such result: certainty1|sentence1|ccue1 certainty2|sentence2|ccue2 ... So the delimiter is |.

But my current code writes everything in 1 row and these delimiters are everywhere:

| |c|e|r|t|a|i|n|

Why does it happen and how can I fix it? Thank you!

when you do that:

if certainty and (ccue is not None):
  writer.writerow('  %s | %s | %s' % (certainty, ''.join(sentence.itertext()), ccue.text))
else:
  writer.writerow('  %s | | %s' % (certainty,sentence.text))

you're passing a str to writerow , which expects an iterable , so it iterates on your string, and you get 1 cell per char.

Note that you don't have to re-specify the separator, it's already set in the csv.writer . You just have to pass your elements as a list or tuple for instance:

if certainty and ccue: # let's simplify your test (ccue is an object or None)
    writer.writerow((certainty,''.join(sentence.itertext()),ccue.text))
else:
    writer.writerow((certainty,'',sentence.text))

EDIT: I had left out your other issue, the 1-line problem. For that one, the context handler + csv writer creation below:

with open('new_file.csv', 'w', newline='') as csvfile:
   writer = csv.writer(csvfile, delimiter='|',
                        quotechar='^', quoting=csv.QUOTE_MINIMAL)

should be put outside the for loop else you'll only see the last line in the end (leaving as-is and using append mode is also possible, but less performant)

writer.writerow需要一个list而不是一个字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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