如何在Python中将csv文件行列值转换为(行,列,值)

时间:2016-07-28 08:56:58

标签: python python-2.7 csv row

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)

我想使用python by array或for循环~~

像这样〜

for x in range(len(row))

2 个答案:

答案 0 :(得分:0)

假设你有这样的example.csv文件:

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))

答案 1 :(得分:0)

with open("t.csv","rb") as open_file:#Read the file
      my_file = open_file.read().decode('utf-8','ignore')

    data = my_file.splitlines()
    data = [r.split('|') for r in data]
    row_len = len(data)
    for i,j in enumerate(data):
        col_len = len(data[0])
        start_index = 0
        while start_index<col_len:
            print (str(i).ljust(2,'0'),str(start_index).ljust(2,'0'),str(data[i][start_index]))
            start_index+=1