简体   繁体   English

从一个文件读取并在Python中写入另一个文件

[英]Reading from one file and writing into another in Python

I'm writing a program that finds certain lines of text in a file and writes them to another file to get rid of all the annoying text in between (it's a .vmg file). 我正在编写一个程序,在文件中找到某些文本行,并将它们写入另一个文件,以消除其间的所有烦人文本(它是.vmg文件)。 Through my skills of python and googleing I have writen the following program. 通过我的python和googleing技能,我编写了以下程序。

with open("filein.txt") as f:
    with open("fileout.txt", "w") as f1:
        for line in f:
            if "telephone number" in line:
                f1.write(line)
            if "telephone number" and "X-BOX" in line:
                f1.write(line)
            if "Subject" in line:
                f1.write(line)

The program is working fine, but for the "subject" I want to tell the program to write not just the single line of text where the keyword "subject" is, but until it reaches a certain keyword (in my case that is "END:VBODY"). 该程序工作正常,但对于“主题”我想告诉程序不仅要写关键字“subject”的单行文本,而是直到它到达某个关键字(在我的情况下是“END”) :VBODY“)。 What should I do? 我该怎么办?

Not the most elegant, but you can just use a flag to indicate whether or not the cursor is in between the Subject and END:VBODY:: 不是最优雅的,但你可以使用一个标志来指示光标是否在Subject和END之间:VBODY ::

with open("filein.txt") as f:
    with open("fileout.txt", "w") as f1:
        for line in f:
            if in_subject:
                f1.write(line)
                if "END:VBODY" in line:
                    in_subject = False
            else:
                if "telephone number" in line:
                    f1.write(line)
                if "telephone number" and "X-BOX" in line:
                    f1.write(line)
                if "Subject" in line:
                    in_subject = True
                    f1.write(line)

If you just need this one particular case, you can walk away with simple state variable. 如果您只需要这个特例,您可以使用简单的状态变量。 Something like this: 像这样的东西:

inside_subject = False
with open("fileout.txt", "w") as out:
    for line in open("filein.txt", "r"):
        if inside_subject:
            if not "END:VBODY" in line:
                print line
            else:
                inside_subject = False
        else:
            if "telephone number" in line:
                out.write(line)
            elif "telephone number" in line and "X-BOX" in line: # will never match this though
                out.write(line)
            elif "Subject" in line:
                out.write(line)
                inside_subject = True

Though if you'll have more conditions, consider using some state machine. 虽然如果你有更多的条件,考虑使用一些状态机。

if "Subject" in line:
    f1.write(line[:line.index("END:VBODY")])

Here is another working solution just by adding a while loop under the if "Subject" in line condition. 这是另一个工作解决方案,只需if "Subject" in line条件下的if "Subject" in line下添加while循环。

with open("filein.txt") as f:
    with open("fileout.txt", "w") as f1:
        for line in f:
            if "telephone number" in line:
                f1.write(line)
            if "telephone number" and "X-BOX" in line:
                f1.write(line)
            if "Subject" in line:
                f1.write(line)

                # new code added here
                while 'END:VBODY' not in line:
                    line = f.next()
                    f1.write(line)

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

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