English 中文(简体)
将文本文件中的多个行断开到列表列表列表中
原标题:Break multiple lines in a text file into a list of lists
  • 时间:2012-05-23 21:22:05
  •  标签:
  • python

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

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 ]

界线之间没有区别。

我怎样才能修改这个来区分每一条线?

最佳回答

拯救行动的理解列表 :

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

返回返回返回

[" important1a ,  important1b ", " important2a ,  important2b "]
问题回答

您可以使用列表理解。 您的代码看起来像 :

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])

不如这样吧:

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

print useful




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签