English 中文(简体)
如何读到包含沙捞越多变数据的档案
原标题:How to read a file with variable multi-row data in Python
  • 时间:2011-11-13 17:36:04
  •  标签:
  • python

I have a file that is about 100Mb that looks like this:

#meta data 1    
skadjflaskdjfasljdfalskdjfl
sdkfjhasdlkgjhsdlkjghlaskdj
asdhfk
#meta data 2
jflaksdjflaksjdflkjasdlfjas
ldaksjflkdsajlkdfj
#meta data 3
alsdkjflasdjkfglalaskdjf

文档中含有一行元数据,数据与数项变量长度数据相对应,仅含有甲型数字特性。 将这些数据列入一个简单清单的最佳方式是:

data = [[#meta data 1, skadjflaskdjfasljdfalskdjflsdkfjhasdlkgjhsdlkjghlaskdjasdhfk],
       [#meta data 2, jflaksdjflaksjdflkjasdlfjasldaksjflkdsajlkdfj],
       [#meta data 3, alsdkjflasdjkfglalaskdjf]]

我的初衷是使用<代码>read()方法将整个卷宗读为记忆,然后使用定期表述将数据按预期格式分类。 是否有更完善的老路? 所有元数据线从环形开始,所有数据线都是甲型数字。 感谢!

最佳回答
问题回答

我不知道这是否最快,但从我头上看:

data = []
with open( input.file ,  r ) as fp:
    for line in fp:
        line = line.strip()
        if line[0] ==  # :
            data.append((line, []))
        else:
            data[-1][1].append(line)
data = [(X,   .join(Y)) for X, Y in data]

我猜想的是:

result = []
for line in file.readlines():
    if line[0] ==  # :
        result.append([line])
    else:
        if len(result[-1]) == 1:
            result[-1].append(line)
        else:
            result[-1][-1] += line

没有测试。

我很简单,例如:

data = [] # result
lastmeta = None # the last metadata line seen
chunks = [] # lines since the last metadata line
for line in input:
    if line[0] ==  # : # metadata
        if lastmeta: # need to flush data we ve collected
            data.append((lastmeta,   .join(chunks))
        lastmeta = line
    else:
        chunks.append(line)




相关问题
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 ]="...

热门标签