我有一张桌子
这在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基本上,我有问题,整理数据的方式,并打印到文本文件没有引号。
发布于 2014-10-04 02:48:50
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)https://stackoverflow.com/questions/26187215
复制相似问题