1

我正在处理一个文本文件,例如:

blahblahblahblahblahblahblah
blahblahblahblahblahblahblah
start
important1a important1b
important2a important2b
end
blahblahblahblahblahblahblah

我想要的是得到一个像

["'important1a', 'important1b'", "'important2a', 'important2b'"]

每条重要的行都被拆分为单独的元素,但它们在一个列表中按行组合在一起。

我已经接近了这个:

import shlex

useful = []
with open('test.txt', 'r') as myfile:
    for line in myfile:
        if "start" in line:
            break
    for line in myfile:
        if "end" in line:
            break       
        useful.append(line)

data = "".join(useful)

split_data = shlex.split(data)
print split_data

这输出:

['important1a', 'important1b', 'important2a', 'important2b']

行之间没有区别。

如何修改它以区分每一行?谢谢!

4

3 回答 3

2

列出理解以进行救援:

[", ".join(map(repr, ln.split())) for ln in open("test.txt")
                                  if "important" in ln]

返回

["'important1a', 'important1b'", "'important2a', 'important2b'"]
于 2012-05-23T21:25:04.317 回答
0

像这样的东西怎么样:

useful = []
for line in open('test.txt'):
    parts = line.split()
    if parts[1:]:
        useful.append("'%s'" % "', '".join(parts))

print useful
于 2012-05-23T21:25:33.863 回答
0

您可以使用列表推导。您的代码如下所示:

useful = []
with open('test.txt', 'r') as myfile:
    for line in myfile:
        if "start" in line:
            break
    for line in myfile:
        line = line.strip()
        if "end" in line:
            break       
        useful.append(line)

print(["'%s'" % ','.join(elem.split(' ')) for elem in useful])
于 2012-05-23T21:30:43.217 回答