我有以下格式的csv文件
Section "Test A",
1,F-1,A-2,D-5,
2,A-1,D-2,E-5,
Section "Test B",
3,C-2,D-1,F-5,
4,D-1,B-2,C-1,
5,E-1,B-3,C-4,
Section "Test C",
6,A-2,D-1,C-4,我正在尝试获得以下输出
Test A
1,F-1,A-2,D-5,
2,A-1,D-2,E-5,
Test B
3,C-2,D-1,F-5,
4,D-1,B-2,C-1,
5,E-1,B-3,C-4,
Test C
6,A-2,D-1,C-4,我能够解析它,但不知道如何获得特定部分的行。
我正在处理的代码
import csv
with open('list.csv', newline='') as csvfile:
lreader = csv.reader(csvfile, delimiter=' ')
for row in lreader:
test = (', '.join(row))
if "Section" in test:
print(test)发布于 2021-08-04 16:01:23
以下是我的建议:
t = open('your_file.csv').readlines()
for i in range(len(t)):
if 'Section' in t[i]:
t[i]=t[i].replace('Section ', '').replace('"', '').replace(',', '')
with open('result.csv', 'w') as f:
f.writelines(t)输出:
Test A
1,F-1,A-2,D-5,
2,A-1,D-2,E-5,
Test B
3,C-2,D-1,F-5,
4,D-1,B-2,C-1,
5,E-1,B-3,C-4,
Test C
6,A-2,D-1,C-4,https://stackoverflow.com/questions/68654367
复制相似问题