0

Python newbie here. I've used XPath to extract some data from a website which gives me a list in a list that looks like this:

[['1','2','3'],['4','','6'],['7','8','9']]

Currently, when I export this to CSV, each of the 'inner' lists is displayed in a single cell like:

Cell A1 ['1', '2', '3']) Cell B1 ['4', '', '6'] Cell C1 ['7', '8', '9']

which means I would have to remove the unwanted brackets and quotes, Text-to-Columns and transpose to get what I want, which is this:

1,4,7
2,,8
3,6,9

What can I do/use to do this?

Community
  • 1
  • 1
emiwark
  • 25
  • 1
  • 7

1 Answers1

0

You can use zip:

import csv
d = [['1','2','3'],['4','','6'],['7','8','9']]
with open('filename.csv', 'w') as f:
   write = csv.writer(f)
   write.writerows(list(zip(*d)))

Output:

1,4,7
2,,8
3,6,9
Ajax1234
  • 69,937
  • 8
  • 61
  • 102