How do I write only content of the inner loop in rows to csv file?
JavaScript
x
5
1
lst=[['apple'], ['banana'], ['cantaloupe'], ['durian']]
2
f = writer(open("abcd.file", 'w', newline = ''), delimiter=",")
3
for row in lst:
4
f.writerow(["xoxo", row, '', '', '', ''])
5
got:
JavaScript
1
5
1
xoxo,['apple'],,,,
2
xoxo,['banana'],,,,
3
xoxo,['cantaloupe'],,,,
4
xoxo,['durian'],,,,
5
want in abcd.file:
JavaScript
1
5
1
xoxo,apple,,,,
2
xoxo,banana,,,,
3
xoxo,cantaloupe,,,,
4
xoxo,durian,,,,
5
Advertisement
Answer
your lst
is a list inside a list, so in order to print the item inside you need to use [0]
try this:
JavaScript
1
5
1
lst=[['apple'], ['banana'], ['cantaloupe'], ['durian']]
2
f = writer(open("abcd.file", 'w', newline = ''), delimiter=",")
3
for row in lst:
4
f.writerow(["xoxo", row[0], '', '', '', ''])
5