繁体   English   中英

Python打印格式

[英]Python Printing format

我有一张桌子

这是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

基本上我有以下问题排序数据并打印到文本文件不带引号的问题

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM