我有 csv 檔案,行中的資訊(id 和文本)如下例所示:
Field1:
Text:
1, A,A,A,B
Field2:
Text:
2, A,B,C,C
我想要的輸出:
Field1: Field2: Field1: Field2:
ID: Text: ID: Text:
1 A 2 A
1 A 2 B
1 A 2 C
1 B 2 C
我怎么能通過 python 做到這一點?謝謝 !
uj5u.com熱心網友回復:
這是一種解決您的問題的方法:
with open('infoo.txt', 'r', encoding="utf-8") as f:
records = []
rows = [[x.strip() for x in row.split(',')] for row in f.readlines()]
i = 0
while i < len(rows):
gotOneOrMoreRows = False
if len(rows[i]) == 1 and rows[i][0].startswith('Field'):
gotOneOrMoreRows = True
i = 1
if i < len(rows) and len(rows[i]) == 1 and rows[i][0] == 'Text:':
gotOneOrMoreRows = True
i = 1
row = rows[i]
records.append([[row[0], row[i]] for i in range(1, len(row))])
if not gotOneOrMoreRows:
i = 1
with open('outfoo.txt', 'w', encoding="utf-8") as g:
tab = '\t'
g.write(f'{tab.join(["Field 1:" tab "Field 2:" for record in records])}\n')
g.write(f'{tab.join(["ID:" tab "Text:" for record in records])}\n')
for i in range(len(records[0])):
g.write(f'{tab.join(colPair[i][0] tab colPair[i][1] for colPair in records)}\n')
# check the output file:
with open('outfoo.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
輸出:
contents of output file:
Field 1: Field 2: Field 1: Field 2:
ID: Text: ID: Text:
1 A 2 A
1 A 2 B
1 A 2 C
1 B 2 C
請注意,使用 pandas 可能會更容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/479368.html
上一篇:用逗號分隔值分割csv中的行
