Python将单列数据转换为多列

0 投票
1 回答
1353 浏览
提问于 2025-04-17 21:49

我有一个.txt文件,里面存储了一些简单的数字数据。这些数据是对同一事物的多次测量,都是以长列的形式写出来的。我想要一个脚本来读取这个文件,识别出分隔不同实验的数据的标记,然后把这些数据写到一个txt或csv文件中的不同列里。

目前,这些数据是通过标记 ' # row = X ' 来分隔的,其中X的值从0到大约128。所以我想要一个脚本,打开这个文件,读取到 'row = 0',然后把接下来的大约1030行数据复制到一个列表或数组中,作为“列0”。然后当它遇到 'row = 1' 时,就把接下来的大约1030行数字复制到“列1”……以此类推。最后,它应该把这些数据写成多列的格式。输入数据文件大致是这样的:

# row = 0
9501.7734375
9279.390625
[..and so on for about 1030 lines...]
8836.5
8615.1640625
# row = 1
4396.1953125
4197.1796875
[..and so on for about 1030 lines...]
3994.4296875
# row = 2
9088.046875
8680.6953125
[..and so on for about 1030 lines...]
8253.0546875

最终的文件应该看起来像这样:

row0          row1         row2       row3
9501.7734375  4396.1953125 etc        etc
9279.390625   4197.1796875
[..snip...]   [...snip...]
8836.5        3994.4296875
8615.1640625  3994.4347453

最好用Python,因为我几年前有一些经验!谢谢大家,
Jon

1 个回答

1
from io import StringIO
from collections import OrderedDict

datastring = StringIO(u"""\
# row = 0
9501.7734375
9279.390625
8615.1640625
# row = 1
4396.1953125
4197.1796875
3994.4296875
# row = 2
9088.046875
8680.6953125
8253.0546875
""")      

content = datastring.readlines()
out = OrderedDict()
final = []

for line in content:
    if line.startswith('# row'):
        header = line.strip('\n#')
        out[header] = []
    elif line not in out[header]:
        out[header].append(line.strip('\n'))


for k, v in out.iteritems():
    temp = (k + ',' + ','.join([str(item) for item in v])).split(',')
    final.append(temp)

final = zip(*final)
with open("C:/temp/output.csv", 'w') as fout:
    for item in final:
    fout.write('\t'.join([str(i) for i in item]))

输出结果:

 row = 0         row = 1        row = 2
9501.7734375    4396.1953125    9088.046875
9279.390625     4197.1796875    8680.6953125
8615.1640625    3994.4296875    8253.0546875

撰写回答