Reputation: 415
I have two lists that look like this:
list1 = ['filename1', 'filename2', 'filename3']
list2 = ['1', '2', ['3', '4', '5']]
How can I write to a csv file that will end up like this?
filename1,1
filename2,2
filename3,3,4,5
I've tried two things but neither work:
with open(outfname, 'wb') as fout:
csv_out = csv.writer(fout)
csv_out.writerows(izip(list1, list2))
This shows:
filename1,['1']
filename2,['2']
filename3,['3','4','5']
I tried adding this prior to writerows:
",".join(list2)
But the results will show:
filename1,1
filename2,2
filename3,"1,2,3"
Upvotes: 2
Views: 125
Reputation: 5907
you can use pandas data-frame instead to form the desired .csv file
import pandas as pd
list1 = ['filename1', 'filename2', 'filename3']
list2 = ['1', '2', ['3', '4', '5']]
df = pd.DataFrame()
df['a'] = list1
df['b'] = list2
df.to_csv('D:/sx.csv',index=False) ####you can give any desired path for csv
TO FLATTEN the list just do
df['b']=[','.join(x) for x in df['b']] ####i have separated the list items by comma as of now, but you can give your own separator
continuing the code
df.to_csv('D:/sx.csv',index=False)
d = pd.read_csv('D:/sx.csv')
d
a b
0 filename1 1
1 filename2 2
2 filename3 3,4,5
Again modifying
d['a'] = d['a']+','+d['b']
d
Out[39]:
a b
0 filename1,1 1
1 filename2,2 2
2 filename3,3,4,5 3,4,5
then drop the column b
df = d.drop('b',axis=1)
df
Out[44]:
a
0 filename1,1
1 filename2,2
2 filename3,3,4,5
######column a shows the desired output...
now you can form the csv by df.to_csv('path+file.csv')
Upvotes: 0
Reputation: 2280
Remember that a string is a one item list. Why do you need pandas and all for this simple task?
list1 = ['filename1', 'filename2', 'filename3']
list2 = ['1', '2', ['3', '4', '5']]
with open(outfname, 'w') as fout:
for item in zip(list1,list2):
fout.write(item[0])
fout.write(',')
fout.write(','.join(item[1]))
fout.write('\n')
Upvotes: 0
Reputation: 17579
You do not need pandas for that. First to get your desired data structure you can just use zip
. To save to CSV you can use standard CSV module.
import csv
list1 = ['filename1', 'filename2', 'filename3']
list2 = ['1', '2', ['3', '4', '5']]
zipped = zip(list1, list2)
with open('some.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(zipped)
Upvotes: 0
Reputation: 21609
Like this. It requires a few temporary lists, but gets the job done.
import csv
list1 = ['filename1', 'filename2', 'filename3']
list2 = ['1', '2', ['3', '4', '5']]
with open('out.csv', 'w') as f:
r = csv.writer(f)
for name, num in zip(list1, list2):
r.writerow([name] + list(num))
out.csv
looks like
filename1,1
filename2,2
filename3,3,4,5
Upvotes: 0
Reputation: 30258
@Satya has a good idea to use pandas but you need to flatten list2, you can do that with .apply()
:
>>> import pandas as pd
>>> import sys
>>> df = pd.DataFrame({'list1': list1, 'list2': list2})
>>> df = df.join(df.list2.apply(lambda x: pd.Series(x)))
>>> df.drop('list2', axis=1).to_csv(sys.stdout, header=False, index=False)
filename1,1,,
filename2,2,,
filename3,3,4,5
This puts empty fields where there is missing data.
Alternatively you can write your own special zip:
def my_zip(l1, l2):
for a, b in zip(l1, l2):
try:
yield [a]+b
except TypeError:
yield [a, b]
>>> csv_out = csv.writer(sys.stdout)
>>> csv_out.writerows(my_zip(list1, list2))
filename1,1
filename2,2
filename3,3,4,5
Which is exactly the output requested.
Upvotes: 2