简体   繁体   English

Python在找到特定行时添加行

[英]Python to add line when a specific line found

I'm having trouble writing a line to txt file when when a specific line is found.当找到特定行时,我无法将一行写入 txt 文件。

Here is my txt file:这是我的txt文件:

Wed Oct  7 09:00:01

station: 01

station: 02

station: 03



Wed Oct  7 09:05:01

station: 06

station: 05
 
station: 06

I need the output txt file look like this:我需要输出 txt 文件如下所示:

Wed Oct  7 09:00:01

Time: Wed Oct  7 09:00:01

station: 01

Time: Wed Oct  7 09:00:01

station: 02

Time: Wed Oct  7 09:00:01

station: 02


Wed Oct  7 09:05:01

Time: Wed Oct  7 09:05:01

station: 04

Time: Wed Oct  7 09:05:01

station: 05

Time: Wed Oct  7 09:05:01

station: 05

When a date is found, it adds a new line with that date before a station.找到日期后,它会在站前添加具有该日期的新行。 If it met a 2nd date, it adds that new date to the next station如果它遇到了第二个日期,则将该新日期添加到下一站

Here is my code:这是我的代码:

date1 = 'Wed Oct 7 09:00:01'
date2 = 'Wed Oct 7 09:05:01'
with open('text.txt') as f_input, open('text2.txt', 'w', newline='') as f_output:
    for line in f_input:
        if date1 in line:
            if line.startswith('station'):
                line = 'Time: ' + date1 + line
        if date2 in line:
            if line.startswith('station'):
                line = 'TIME: ' + date2 + line
    f_output.write(line)

When I run the code, the result is only "station 06".当我运行代码时,结果只是“station 06”。 Please let me know how to fix this.请让我知道如何解决这个问题。 Thank you.谢谢你。

您只向文件写入一行,检查代码中最后一行的缩进,它不在 for 循环内。

This script should get the output you expect.这个脚本应该得到你期望的输出。 There is an indentation error in your code and the if condition is not correct.您的代码中存在缩进错误,并且 if 条件不正确。 Compare the code and you will understand.对比一下代码你就明白了。

import re

date = None
date_regex = "[a-z]{3}[\ ]+[a-z]{3}[\ ]+[0-9]{1,2}[\ ]+[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}"
with open('text.txt') as f_input, open('text2.txt', 'w', newline='') as f_output:
    for line in f_input:
        date_match = re.match(date_regex, line, re.I)
        if date_match:
            date = date_match.group()
        if date and date not in line and line.startswith('station'):
            line = 'Time: ' + date + '\n\n' + line
        f_output.write(line)

I have removed the file part for obvious reasons.出于显而易见的原因,我删除了文件部分。

lines = """Wed Oct  7 09:00:01
station: 01
station: 02
station: 03
Wed Oct  7 09:05:01
station: 06
station: 05 
station: 06""".split("\n")

pattern = """^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s*\d\s*\d{2}[\:]\d{2}[\:]\d{2}"""


output = ""
last_match = ""
for line in lines:
    message = line + "\n"
    if re.match(pattern, line):
        last_match = line 
    else:
        if last_match:
            message = "Time: " + last_match + "\n" + message
    output = output + message

print(output.rstrip("\n")) # if you must remove the last EOL        
        

# output
Wed Oct  7 09:00:01
Time: Wed Oct  7 09:00:01
station: 01
Time: Wed Oct  7 09:00:01
station: 02
Time: Wed Oct  7 09:00:01
station: 03
Wed Oct  7 09:05:01
Time: Wed Oct  7 09:05:01
station: 06
Time: Wed Oct  7 09:05:01
station: 05 
Time: Wed Oct  7 09:05:01
station: 06

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

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