How can I change a list of strings into CSV in Python?
Example strings:
uji708
uhodih
utus29
agamu4
azi340
ekon62
I need to change them into CSV list like this:
uji708,uhodih,utus29,
agamu4,azi340,ekon62,
My code so far:
email = 'mail_list.txt'
handle = open(email)
for line in handle:
try:
email = line.split()[0].replace('\n', '')
l = line.split()
开发者_运维技巧 print '\n'.join((','.join(x) for x in zip(l[::3], l[1::3], l[2::3])))
except:
print 'error'
How can I do this in Python?
Use csv.writer:
import csv
import sys
writer = csv.csvwriter(sys.stdout)
writer.writerow(iterable_containing_my_strings)
Here is a very specific answer to a very specific question when you will clarify/generalize your question I may update my answer
s = """
uji708
uhodih
utus29
agamu4
azi340
ekon62
"""
l = s.split()
print '\n'.join((','.join(x) for x in zip(l[::3], l[1::3], l[2::3])))
精彩评论