【问题标题】:how to skip blocks of text when writing file in python在python中写入文件时如何跳过文本块
【发布时间】:2015-03-20 06:59:04
【问题描述】:

在从另一个文件写入文件时,是否可以使用 python 跳过文本块?

例如,假设输入文件是:

This is the file I would like to write this line
I would like to skip this line
and this one...
and this one...
and this one...
but I want to write this one
and this one...

我如何编写一个脚本,让我跳过某些内容和大小不同的行,一旦识别到某个行,它就会继续将这些行写入另一个文件?

编辑

我的代码通读行,不写重复行,并使用字典和正则表达式对行执行一些操作。

【问题讨论】:

  • 这完全取决于您要跳过这些行的为什么。需要有一些可编码的 rule 来选择某些行而不是其他行。假设你有这个规则,这个程序真的变得非常简单......

标签: python file input output writing


【解决方案1】:

伪代码:

# Open input and output files, and declare the unwanted function
for line in file1:
    if unwanted(line):
        continue
    file2.write(line)
# Close files etc...

【讨论】:

    【解决方案2】:

    您可以逐行读取文件,并控制您读取的每一行:

    with open(<your_file>, 'r') as lines:
        for line in lines:
            # skip this line
            # but not this one
    

    请注意,如果您想阅读所有行而不考虑内容,然后才对其进行操作,您可以:

    with open(<your_file>) as fil:
        lines = fil.readlines()
    

    【讨论】:

      【解决方案3】:

      这应该可行:

      SIZE_TO_SKIP = ?
      CONTENT_TO_SKIP = "skip it"
      
      with open("my/input/file") as input_file:
          with open("my/output/file",'w') as output_file:
              for line in input_file:
                  if len(line)!=SIZE_TO_SKIP and line!=CONTENT_TO_SKIP:
                      output_file.write(line)
      

      【讨论】:

        【解决方案4】:
        def is_wanted(line):
            #
            # You have to define this!
            #
            # return True to keep the line, or False to discard it
        
        def copy_some_lines(infname, outfname, wanted_fn=is_wanted):
            with open(infname) as inf, open(outfname, "w") as outf:
                outf.writelines(line for line in inf if wanted_fn(line))
        
        copy_some_lines("file_a.txt", "some_of_a.txt")
        

        为了将其扩展到多行块,您可以实现一个有限状态机,如

        这会变成类似的东西

        class BlockState:
            GOOD_BLOCK = True
            BAD_BLOCK = False
        
            def __init__(self):
                self.state = self.GOOD_BLOCK
        
            def is_bad(self, line):
                # *** Implement this! ***
                # return True if line is bad
        
            def is_good(self, line):
                # *** Implement this! ***
                # return True if line is good
        
            def __call__(self, line):
                if self.state == self.GOOD_BLOCK:
                    if self.is_bad(line):
                        self.state = self.BAD_BLOCK
                else:
                    if self.is_good(line):
                        self.state = self.GOOD_BLOCK
                return self.state
        

        然后

        copy_some_lines("file_a.txt", "some_of_a.txt", BlockState())
        

        【讨论】:

        • 这就是我要找的东西!
        猜你喜欢
        • 2016-01-31
        • 2021-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        相关资源
        最近更新 更多