简体   繁体   English

Python 如何从一个文件中剪切一定数量的行并将它们写入另一个

[英]Python how to cut a certain number of lines from a file and write them into another

I'm starting to learn python for everyday tasks, and up to this point have only encountered simple file processing.我开始为日常任务学习python,到目前为止只遇到过简单的文件处理。 But today the task of processing a large number of heavy files came up before me and I ended up with.但是今天处理大量繁重文件的任务摆在我面前,我最终完成了。

I have a large file with the following contents:我有一个包含以下内容的大文件:

TRACE:1
10000.000000;-4.316597;
10050.000000;-4.686951;
10100.000000;-5.178696;
10150.000000;-4.356827;
10200.000000;-4.620125;
.....
TRACE 2:
10000.000000;-50.371719;
10050.000000;-52.572052;
10100.000000;-60.795563;
10150.000000;-50.679413;
10200.000000;-62.036072;
.....
TRACE 3:
10000.000000;-10.796394;
10050.000000;-10.879318;
10100.000000;-11.238129;
10150.000000;-10.811073;
10200.000000;-10.627502;
10250.000000;-10.825951;
10300.000000;-11.240158;
TRACE 4:
Nope;Nope;

I need to separate the trace data into separate files我需要将跟踪数据分成单独的文件

Input: data.DAT --> Output: Trace1.DAT, Trace2.DAT, Trace3.DAT

I tried to do it in this way我试着用这种方式做

import os
os.chdir('C:\path\to\work\dir')
with open('data.DAT') as dataFile, \
        open('Trace1.DAT', "w+") as T1,\
        open('Trace2.DAT', "w+") as T2,\
        open('Trace3.DAT', "w+") as T3:
    a = []
    for num, line in enumerate(dataFile, 1):
        if 'TRACE 1:' in line:
            a.append(num)
        if 'TRACE 2:' in line:
            a.append(num)
        if 'TRACE 3:' in line:
            a.append(num)
        if 'TRACE 4:' in line:
            a.append(num)
    print(a)
    [dataFile.readline() for _ in range(a[0], a[1])]
    T1.writelines(dataFile)
    [dataFile.readline() for _ in range(a[1], a[2])]
    T2.writelines(dataFile)
    [dataFile.readline() for _ in range(a[2], a[3])]
    T3.writelines(dataFile)

And I got Python output:我得到了 Python output:

a = [42, 1999847, 3999652, 5999457]

Created files:创建的文件:

drwxrwxrwx 1 user user       512 Jan 10 17:31 .
drwxrwxrwx 1 user user       512 Jan 10 17:31 ..
-rwxrwxrwx 1 user user         0 Jan 10 16:46 Trace1.DAT
-rwxrwxrwx 1 user user         0 Jan 10 16:47 Trace2.DAT
-rwxrwxrwx 1 user user         0 Jan 10 16:47 Trace3.DAT
-rwxrwxrwx 1 user user 173082112 Jan 11  2019 data.DAT

Thanks for the help or advice感谢您的帮助或建议

You can try:你可以试试:

with open('data.DAT') as inp:
    out = None
    for line in inp:
        if line.startswith('TRACE'):
            if out != None:
                out.close()
            filename = f"Trace{line.split()[1][:-1]}.DAT"
            out = open(filename, 'w')
        else:
            out.write(line)
    out.close()

a bit lengthy but works:)有点长但有效:)

with open('data.DAT', 'r') as filereader:使用 open('data.DAT', 'r') 作为文件阅读器:

read = filereader.readlines()

traceOne = None
TraceTwo = None
TraceThree = None
for i in read:
    if i.startswith("TRACE:1"):
        traceOne = read.index(i)
    elif i.startswith("TRACE 2:"):
        TraceTwo = read.index(i)
    elif i.startswith("TRACE 3:"):
        TraceThree = read.index(i)
    else:
        continue

with open('TRACE1.DAT', 'w+') as writeTraceONE:
    traceonefile = writeTraceONE.writelines("\n".join(read[traceOne:TraceTwo]))

with open('TRACE2.DAT', 'w+') as writeTraceTwo:
    tracetwofile = writeTraceTwo.writelines("\n".join(read[TraceTwo:TraceThree]))

with open('TRACE3.DAT', 'w+') as writeTraceThree:
    traceThreeFile = writeTraceThree.writelines("\n".join(read[TraceThree:]))

暂无
暂无

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

相关问题 如何使用 python 从文件中剪切一些行并将它们粘贴到同一文件中的某个 position? - How to cut some lines from a file and paste them at certain position in a same file using python? Python从读取文件中拆分行,然后在另一文件中以另一种格式写入它们 - Python split lines from read file and write them with another format in another file 如何使用Python中的函数搜索文件中的特定行并将其写入另一个文件 - How to search specific lines in a file and write them to another file, using function in Python 如何将一个文件的最后50行写入另一个Python - How to write last 50 lines from one file to another Python Python从文件读取行,然后从特定的行号写入另一个文件 - Python read lines from a file and write from a specific line number to another file 如何使用python复制一个文件中的行并将其写入另一文件中? - How to copy lines from one file and write them in other file using python? 读取行并在python中以另一种格式写入它们 - Reading lines and write them in another format in python 如何从 a.txt 文件中读取某些字符并将它们写入 Python 中的 a.csv 文件? - How can I read certain characters from a .txt file and write them to a .csv file in Python? 如何在Python中仅将某些行写入文件? - How do I write only certain lines to a file in Python? Python - 从日志文件中提取字符串并将它们写入另一个文件 - Python - Extract strings from a log file and write them into another file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM