Markwin - 6 months ago 39

Python Question

Is it possible to read

`csv`

e.g. if i have a csv file like this:

a b c

1 0 1

1 4 1

How would one get a list of

`[a,1,1],[b,0,4],[c,1,1]`

Answer

You are looking for transpose functionality. To solve your problem,

- First read csv as rows and create row wise tuples in a list
ex:
`[(a,b,c),(1,0,1),(1,4,1)..]`

- Transpose above list using any of the solutions present in Transpose a matrix in Python.

Post transpose , your data will look like `[(a,1,1),(b,0,4)..]`

Source (Stackoverflow)