Python:读取和写入带标题和数值列的文本文件

0 投票
1 回答
7303 浏览
提问于 2025-04-18 05:48

我有一个文本文件,这个文件里有一些标题行和一些数字列。我想用一个Python脚本来读取这个文件,跳过标题部分,然后选择一些列,把它们写入一个新文件。

比如,我们把下面的数据叫做 in_table.txt。我想跳过标题(还有空行),然后选择第一列和第四列(只要数字),最后把它们保存到一个新文件 out_table.txt 中,不需要标题,只要数字就行。请问我该怎么用Python脚本来实现这个呢?

非常感谢!!

in_table.txt:

hline1 hline1 hline1
hline2 hline2 hline2

hline3 hline3 hline3

par1  par2  par3  par4  par5
1.    10.   100.  1000. 10000.
2.    20.   200.  2000. 20000.
3.    30.   300.  3000. 30000.
4.    40.   400.  4000. 40000.
5.    50.   500.  5000. 50000.

.

1 个回答

2

如果你选择用空格来分隔的话,可以使用

with open('in_table.txt') as f:
    # Iterate through the file until the table starts
    for line in f:
        if line.startswith('------'):
            break
    # Read the rest of the data, using spaces to split. 
    data = [r.split() for r in f]

with open('out_file.csv', 'w') as of:
    for r in data:
        # Write only column 0 and 2 (re: 0-indexing)
        of.write('%s, %s\n'%(r[0], r[2]))

CSV

如果你用逗号来分隔的话,可能可以用Python自带的csv

import csv 
with open('in_table.txt') as f:
    for line in f:
        if line.startswith('------'):
            break
    data = [r for r in csv.reader(f)]

with open('out_file.csv', 'w') as of:
    for r in data:
        of.write('%s, %s\n'%(r[0], r[2]))

或者也许可以更简洁地

import csv 
with open('in_table.txt') as f:
    for line in f:
        if line.startswith('------'):
            break  
    data = [r[0]+r[2] for r in csv.reader(f)]

wrt = csv.writer(open('out_file.csv', 'w'))
wrt.writerows(data)

撰写回答