我有一张像
这样的表格这是CSV文件(我在python中读取)
Temp E1 E2 n
100 3000 500 0.31
200 4000 521 0.32
300 5000 522 0.33
我想使用Python
在txt文件中打印以下输出TableP1,101,,,,,,,100,3000,200,4000,300,5000,end.
TableP1,102,,,,,,,100,500,200,521,300,522,end
TableP1,103,,,,,,,100,0.31,200,0.32,300,0.33,end
基本上我有问题按照以下方式排序数据并打印到没有引号的文本文件
答案 0 :(得分:0)
with open('1.csv') as f:
column_count = len(next(f).split(','))
f.seek(0) # If there's no header
rows = [line.strip().split(',') for line in f]
with open('2.txt', 'w') as f:
for idx in range(1, column_count):
n = str(100 + idx)
r = ['TableP1', n, '', '', '', '', '', '', '']
for row in rows:
if len(row) < column_count:
continue
r.append(row[0])
r.append(row[idx])
r.append('end')
print ','.join(r)
print >>f, ','.join(r)