Ian Chang Ian Chang - 4 months ago 8
Python Question

How can I turn csv file row column value into (row, column, value) in Python

for example, I read this 3x3 csv file.

01 02 03

01 | 11 | 22 | 33 |

02 | 44 | 55 | 66 |

03 | 77 | 88 | 99 |


Then ,I want to output a new textfile like this photo.

→ (row, column, value)

→ (01, 01, 11)

→ (01, 02, 22)

→ (01, 03, 33)

→ (02, 01, 44)


I want to use python by array or for loop ~~

like this ~

for x in range(len(row))

Answer

suppose you have example.csv file like this:

11|22|33
44|55|66
77|88|99

with open("example.csv") as handler:
    for r,l in enumerate(handler):
        for col, e in enumerate(l.split('|')):
            print('row: %s, col %s, value: %s' % (r+1, col+1, e))