【问题标题】:Python - Reading multiple lines into listPython - 将多行读入列表
【发布时间】:2009-06-30 07:11:37
【问题描述】:

好吧,伙计们/女孩们再次坚持简单的事情
我有一个文本文件,每个条目有多行,数据格式如下

第一个单词单词单词
wordx word word word interesting1 word word word word
字字字字字字句
wordz word word word interesting2 word word word lastword

这个序列重复了一百次左右,除了interesting1和interesting2之外,所有其他单词都相同,没有空行。有趣的 2 与有趣的 1 相关,但与其他任何东西无关,我想将这两个有趣的项目链接在一起,丢弃其余的,例如

有趣1 = 有趣2
有趣1 = 有趣2
有趣1 = 有趣2
等等,每个序列 1 个 lne

每一行都以不同的单词开头
我的尝试是读取文件并执行“if wordx in line”语句来识别第一个有趣的行,切出值,找到第二行,(“if wordz in line)切出值并将第二个与第一个。
不过这很笨拙,我不得不使用全局变量、临时变量等,而且我确信必须有一种方法可以识别 firstword 和 lastword 之间的范围并将其放入一个列表中,然后将两个值切分在一起。

感谢您的任何建议,感谢您的宝贵时间

【问题讨论】:

  • 如果您有一些示例代码可能会有所帮助。很难说你期望如何确定什么是“有趣的”。它总是在索引 4 中吗?
  • 正如蒙库特所说,您试图过度简化问题的内容,不幸的是,这使得您很难理解您想要做什么或为什么要这样做。不要害怕包含一些真实数据,以便我们了解正在发生的事情。
  • 对不起。有趣的词是单个文本词,总是在相同的位置,用空格分隔。措辞可能被视为敏感,因此用香草词代替。如果interest1 是一个国家保险号码,而interest2 是一个状态,那么剩下的就是个人数据,这样就清楚了吗?
  • @Household:请不要评论您自己的问题。请通过包含示例代码和预期输出来更新您的问题。

标签: python text parsing line


【解决方案1】:
from itertools import izip, tee, islice

i1, i2 = tee(open("foo.txt"))

for line2, line4 in izip(islice(i1,1, None, 4), islice(i2, 3, None, 4)) :
    print line2.split(" ")[4], "=", line4.split(" ")[4]

【讨论】:

    【解决方案2】:

    我已经投入了一大堆断言来检查您的数据布局的规律性。

    C:\SO>type words.py
    
    # sample pseudo-file contents
    guff = """\
    firstword word word word
    wordx word word word interesting1-1 word word word word
    wordy word word word
    wordz word word word interesting2-1 word word word lastword
    
    miscellaneous rubbish
    
    firstword word word word
    wordx word word word interesting1-2 word word word word
    wordy word word word
    wordz word word word interesting2-2 word word word lastword
    firstword word word word
    wordx word word word interesting1-3 word word word word
    wordy word word word
    wordz word word word interesting2-3 word word word lastword
    
    """
    
    # change the RHS of each of these to reflect reality
    FIRSTWORD = 'firstword'
    WORDX = 'wordx'
    WORDY = 'wordy'
    WORDZ = 'wordz'
    LASTWORD = 'lastword'
    
    from StringIO import StringIO
    f = StringIO(guff)
    
    while True:
        a = f.readline()
        if not a: break # end of file
        a = a.split()
        if not a: continue # empty line
        if a[0] != FIRSTWORD: continue # skip extraneous matter
        assert len(a) == 4
        b = f.readline().split(); assert len(b) == 9
        c = f.readline().split(); assert len(c) == 4
        d = f.readline().split(); assert len(d) == 9
        assert a[0] == FIRSTWORD
        assert b[0] == WORDX
        assert c[0] == WORDY
        assert d[0] == WORDZ
        assert d[-1] == LASTWORD
        print b[4], d[4]
    
    C:\SO>\python26\python words.py
    interesting1-1 interesting2-1
    interesting1-2 interesting2-2
    interesting1-3 interesting2-3
    
    C:\SO>
    

    【讨论】:

      【解决方案3】:

      在这种情况下,创建一个匹配重复文本的正则表达式,并为有趣的位设置组。然后你应该可以使用 findall 找到所有有趣1 和有趣2 的案例。

      像这样: 重新导入

      text = open("foo.txt").read()
      RE = re.compile('firstword.*?wordx word word word (.*?) word.*?wordz word word word (.*?) word', re.DOTALL)
      print RE.findall(text)
      

      尽管如 cmets 中所述,islice 绝对是一个更简洁的解决方案。

      【讨论】:

      • 假设您的意思是一个四行 re.VERBOSE 样式的正则表达式,第二行类似于 \s* wordx \S+ \s+ \S+ \s+ \S+ \s+ (\S+) \s+ \S+ \s+ \S+ \s+ \S+ \s+ \S+ \s* \n ... OP可能需要一些帮助。用一些解释和如何调整它来说明它应该让你至少获得一个赞成票;-)
      • 嗯...不,您只需要一个与相关文本实际匹配的正则表达式,但不匹配其中的部分或多次重复。我不认为它需要四行在 \s+ 上包含长行...无论如何,islice 是一个更好的解决方案。尽管如此,我还是用完整的解决方案对其进行了更新。
      猜你喜欢
      • 2019-07-31
      • 1970-01-01
      • 2019-02-19
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多