簡體   English   中英

如何在Python中將csv文件行列的值轉換為(行,列,值)

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

例如,我閱讀了此3x3 csv文件。

       01   02   03

01 | 11 | 22 | 33 |

02 | 44 | 55 | 66 |

03 | 77 | 88 | 99 |

然后,我要輸出一個新的文本文件,如這張照片。

→ (row, column, value)

→ (01, 01, 11)

→ (01, 02, 22)

→ (01, 03, 33)

→ (02, 01, 44)

我想通過數組或for循環使用python ~~

這樣〜

for x in range(len(row))

假設您有如下的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))
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM