Python - 将多行读入列表

时间:2022-05-10 21:52:07

OK guys/gals stuck again on something simple
I have a text file which has multiple lines per entry, the data is in the following format

OK guys / gals再次陷入简单的事情我有一个文本文件,每个条目有多行,数据采用以下格式

firstword word word word
wordx word word word interesting1 word word word word
wordy word word word
wordz word word word interesting2 word word word lastword

第一字字字wordx字字字有趣1字字字字罗嗦字字word字字字有趣字2字字字尾字

this sequence repeats a hundred or so times, all other words are the same apart from interesting1 and interesting2, no blank lines. The interesting2 is pertinent to interesting1 but not to anything else and I want to link the two interesting items together, discarding the rest such as

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

interesting1 = interesting2
interesting1 = interesting2
interesting1 = interesting2
etc, 1 lne per sequence

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

Each line begins with a different word
my attempt was to read the file and do an "if wordx in line" statement to identify the first interesting line, slice out the value, find the second line, ("if wordz in line) slice out the value and concatenate the second with the first.
It's clumsy though, I had to use global variables, temp variables etc, and I'm sure there must be a way of identifying the range between firstword and lastword and placing that into a single list, then slicing both values out together.

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

Any suggestions gratefully acknowledged, thanks for your time

任何建议都表示感谢,感谢您的时间

3 个解决方案

#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


I've thrown in a bagful of assertions to check the regularity of your data layout.

我已经抛出一大堆断言来检查数据布局的规律性。

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


In that case, make a regexp that matches the repeating text, and has groups for the interesting bits. Then you should be able to use findall to find all cases of interesting1 and interesting2.

在这种情况下,请创建一个与重复文本匹配的正则表达式,并为有趣位提供组。然后你应该能够使用findall来查找所有有趣的1和有趣的2。

Like so: import re

像这样:import re

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)

Although as mentioned in the comments, the islice is definitely a neater solution.

虽然如评论中所述,islice绝对是一个更简洁的解决方案。

#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


I've thrown in a bagful of assertions to check the regularity of your data layout.

我已经抛出一大堆断言来检查数据布局的规律性。

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


In that case, make a regexp that matches the repeating text, and has groups for the interesting bits. Then you should be able to use findall to find all cases of interesting1 and interesting2.

在这种情况下,请创建一个与重复文本匹配的正则表达式,并为有趣位提供组。然后你应该能够使用findall来查找所有有趣的1和有趣的2。

Like so: import re

像这样:import re

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)

Although as mentioned in the comments, the islice is definitely a neater solution.

虽然如评论中所述,islice绝对是一个更简洁的解决方案。