Python:有没有一种方法可以提取并连接几个系列的文本文件,在我浏览时会删除每个文件的前3行?

问题描述 投票:0回答:2

我有一个文件夹,其中包含多个系列的文本文件,每个文件都包含来自某些分析的一行残差。他们的文件名是这样的:

'residual_x01'
'residual_x02'
...
'residual_y01'
'residual_y02'
...
'residual_z01'
'residual_z02'

文件内容看起来像这样:

1 ### This is the file number in the series
c:\file\location\goes\here
983 1051 0 0 983 1051 ### other identifier
1.1 ### this is where the data I want starts
3.5
0.8
0.7
1.3
... ## so on for about a million lines.

[使用Python,我想从这些文件中提取残差,并连接起来以形成每个系列的一个长文件(即x,y,z),并在我进行操作时删除每个文件的前三行,即形成这个:

1.1 ### data from first file of series 'residual_x01 / _y01 / _z01'
3.5
0.8
0.7
1.3
...
1.1 ### data from second file of series 'residual_x02 / _y02 / _z02'
3.5
0.8
0.7
1.3
...
1.1 ### data from third file of series 'residual_x03 / _y03 / _z03'
3.5
0.8
0.7
1.3
... ... and so on.

我对此不知所措,有人可以帮忙吗?

python
2个回答
0
投票

您没有提供太多数据,所以我做了一些虚假数据。我不想制作一堆文件,所以我只制作了三个假数据文件,但是该代码应适用于任意数量的文件,每个文件的长度也可以是可变的。

假设您具有以下三个文本文件:

files / residual_x01.txt

1
c:\file\location\goes\here
983 1051 0 0 983 1051
1.1
3.5
0.8
0.7
1.3

files / residual_x02.txt

2
c:\file\location\goes\here
983 1051 0 0 983 1051
7.1
8.4
0.3
2.3
0.1

files / residual_y01.txt

1
c:\file\location\goes\here
983 1051 0 0 983 1051
4.2
4.3
1.3
0.2
0.0

代码:

def get_file_lines(path_to_file):

    from itertools import islice

    number_of_lines_to_skip = 3

    with path_to_file.open("r") as file:

        _ = list(islice(file, number_of_lines_to_skip))
        for line in file:
            yield line.strip()


def get_all_floats(path_to_dir):

    from pathlib import Path

    for path in Path(path_to_dir).glob("residual_*.txt"):
        for line in get_file_lines(path):
            yield float(line)


def main():

    for f in get_all_floats("files/"):
        print(f)

    return 0


if __name__ == "__main__":
    import sys
    sys.exit(main())

输出:

1.1
3.5
0.8
0.7
1.3
7.1
8.4
0.3
2.3
0.1
4.2
4.3
1.3
0.2
0.0
>>> 

0
投票

对于每个系列,您可以使用以下代码创建一个文件,其中包含文件中所有行,但每个行的前三行除外:

filenames = ['residual_x01', 'residual_x02', ...]
output_file = 'path/to/output/residual_x'
lines_to_skip = 3
with open(output_file, 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            lines = infile.readlines()[lines_to_skip:]
            for line in lines:
                outfile.write(line)

根据需要更改filenames列表和output_file。您也可以调整lines_to_skip变量。

© www.soinside.com 2019 - 2024. All rights reserved.