【问题标题】:Read a broken line from text file从文本文件中读取虚线
【发布时间】:2012-12-16 19:17:35
【问题描述】:

如果我有这样的文本文件:

[001]This is line 1.
[002][too long]This is line 2 but it's Tooooo
oooo long!
[003]This is line 3.

我写了一个'for line in fileA'来读取这个文件,比如:

for line in fileA:
    ...

现在我需要在 line.find("[too long]")>=0 时合并当前行和下一行。 我该怎么办?

PS: 我写道:

for line in fileA:
    if line.find("[too long]")>=0:
        loc = fileA.tell()
        fileB = open("file.txt") #open this file again
        fileB.seek(loc)
        line += fileB.readline().strip()

但它不起作用。为什么?

【问题讨论】:

  • 您不能两次打开同一个文件,请发布错误消息/堆栈跟踪,“没有”到底是什么工作?您不太清楚自己要做什么,这阻碍了我们的帮助。
  • 遍历行,维护一个缓冲区。当一行以[...] 开头时,产生并清除缓冲区的内容,然后追加新内容。当一行不是以[...] 开头时,将其附加到缓冲区中。

标签: python file iterator


【解决方案1】:

额外读取文件听起来开销太大。试试这个:

with open('file.txt') as f:
    for line in f:
        if '[too long]' in line:
            line = line.rstrip('\r\n') + next(f)
        print line

打印

[001]This is line 1.

[002][too long]This is line 2 but it's Tooooooooo long!

[003]This is line 3.

如果在一行中找到[too long],则会附加以下行。也许您想追加所有其他行,直到一行以 [xxx] 之类的开头?

【讨论】:

    【解决方案2】:

    您可以使用列表推导来获取列表中的所有行,执行与 eumiros 答案非常相似的操作。

    with open('file.txt') as f:
        lines = [line.rstrip('\r\n') + next(f) if '[too long]' in line else line for line in f]
    

    那么输出是:

    >>> lines
        ['[001]This is line 1.\n', "[002][too long]This is line 2 but it's Tooooooooo long!\n", '[003]This is line 3.\n']
    

    【讨论】:

      【解决方案3】:

      我不确定实际文件的样子,但我可能会选择this

      contents = """[001]This is line 1.
      [002][too long]This is line 2 but it's Tooooo
      oooo long!
      [003]This is line 3.
      """
      
      lines = iter( contents.split("\n") )
      
      def fix_file( lines ):
          prev = ''
          number = 1
          for line in lines:
              if not line.startswith( '[{0:03d}]'.format( number ) ):
                  prev += line
              else:
                  yield prev
                  number = number + 1
                  prev = line
          yield prev
      
      for line in fix_file( lines ):
          print line
      

      这样你就不需要额外的内容了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 2022-11-11
        • 2012-10-09
        • 2020-02-24
        • 2014-05-24
        • 2012-10-08
        相关资源
        最近更新 更多