简体   繁体   English

如何在循环添加的文件中打印特定行?

[英]How to print specific lines in a file that was appended from a loop?

from itertools import zip_longest 
f = open("all_info.txt", "a")
with open("all_info.txt") as f, open ("over_speeding.txt") as f1, open("fine.txt") as fine, open("all details.txt", "a") as everything:
     for fline, fineline in zip_longest (f, fine, fillvalue=""): 
          everything.write (fineline.strip() + "   ---   " + fline.strip() + "\n")      


          a = open("all details.txt", "r")
          for line in everything:#the problem
               everything.strip()
               if line in f1:
                    with ("fine1.txt", "a") as fine1:
                         fine1.write(line)

This is just a part of my whole code. 这只是我整个代码的一部分。 My whole code takes vehicle registration number then checks if it is standard or not. 我的整个代码都带有车辆登记号,然后检查它是否标准。 then time taken to travel 1 mile is entered which is used to calculate the speed. 然后输入行驶1英里所需的时间,该时间用于计算速度。

if the speed is greater than 70mph the registration number and the speed the vehicle was travelling goes to the file over_speeding.txt. 如果速度大于70mph,则注册号和车辆行驶的速度将进入文件over_speeding.txt。 then the other conditions determine which file does the details go (but this is not important). 然后由其他条件确定详细信息将转到哪个文件(但这并不重要)。

The code that i have shown you will open 4 files, where the fine.txt file is already set and the over_speeding.txt files gets its data from my whole code. 我向您显示的代码将打开4个文件,其中已经设置了fine.txt文件,并且over_speeding.txt文件从我的整个代码中获取其数据。 all_info.txt file will store all the inputs from my whole code then using the zip_lingest module all_info.txt file is appended with fine.txt to all_details.txt file. all_info.txt文件将存储整个代码中的所有输入,然后使用zip_lingest模块将all_info.txt文件与fine.txt附加到all_details.txt文件中。

what i want just to select the lines in all details.txt that are overspeeding, then save it to fine1.txt 我只想在所有details.txt中选择超速的行,然后将其保存到fine1.txt

eg fine.txt 例如fine.txt

11111
22222
33333
44444

all_info.txt all_info.txt

xxxxxxxx
dddddddd
aaaaaaaa
cccccccc

all_details.txt all_details.txt

11111 --- xxxxxxxx
22222 --- dddddddd
33333 --- aaaaaaaa
44444 --- cccccccc

if 如果

xxxxxxx 

and

aaaaaaa 

were from the file over_speeding.txt, then in the all details.txt file: 来自文件over_speeding.txt,然后位于所有details.txt文件中:

11111 --- xxxxxxxx
33333 --- aaaaaaaa

should be saved into fine1.txt file 应该保存到fine1.txt文件中

I had problems understanding your code (the names for variables are not really helpful, when someone tries to read your code, at least for me), but I've tried to rewrite it doing what you expected it to do: 我在理解您的代码时遇到了问题(至少在我看来,当有人尝试读取您的代码时,变量的名称并没有真正的帮助),但是我尝试按照您的预期进行重写:

from itertools import zip_longest
#EDIT why open all_info.txt twice?
#f = open("all_info.txt", "a")
with open("all_info.txt", "a") as f,\
    open("over_speeding.txt") as f1,\
    open("fine.txt") as fine,\
    open("all details.txt", "a") as everything,\
    open("fine1.txt", "a") as fine1: # moved opening fine1 here

    for fline, fineline in zip_longest(f, fine, fillvalue=""):
        #EDIT
        line_to_write = fineline.strip() + "   ---   " + fline.strip() # so we don't need to write it twice
        everything.write(line_to_write + "\n") # write to file with newline
        if line_to_write in f1: # compare the line with values in over_speeding
            fine1.write(line_to_write)

As it is only part of a script and I don't have all the data I could not test it. 由于它只是脚本的一部分,并且我没有所有无法测试的数据。 Your approach: write all the lines to everything and then check all the lines separately. 您的方法:将所有行写到所有内容,然后分别检查所有行。 My approach: get the line, write it and check it in the same loop. 我的方法:获取该行,编写并在同一循环中检查它。 If I understood your code, it should do the same. 如果我了解您的代码,则应该执行相同的操作。 Please ask or point to any mistakes/misunderstandings. 请询问或指出任何错误/误解。

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

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