English 中文(简体)
Appending lists from files to a single list in Python
原标题:

I m trying to write a function that reads files from a "deferred" directory which contains files that contain lists. Here s what the files in the deferred folder contain:

 173378981 ,  45000 ,  343434 ,  3453453 ,  34534545 ,  3452342 ,  234234 ,  42063008 ,  Exempted ,  10000 
 1000014833 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  Exempted ,  0 
 1000009598 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  Exempted ,  0 
 279483421 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  Exempted ,  0 
 1000009600 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  Exempted ,  0 
 389453080 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  Exempted ,  0 
 1000009602 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  Exempted ,  0 

The function used to write the file(s):

def storeDeferredRecords(records):
    """docstring for createFile"""
    now = datetime.datetime.now()
    filename = deferredDir +  /  + now.strftime("%Y%m%d-%H%M%S")
    f = open(filename,  w )
    newlist = map(lambda(x): str(x)[1:-1], records)
    for item in newlist:
        f.write("%s
" % item)
    f.close

I need help with the function used to read the file. I was only able to write this:

def getDeferredRecords():
        """docstring for getDeferredRecords"""
        infiles = [infile for infile in glob.glob(deferredDir +  /* )]
                <code to read the contents of each file here>

Can someone help me out? I need to read the lines and insert them into a list. This list will then be merged with records from separate CSV file.

最佳回答

See the csv module:

BigList = []
for filename in glob.glob(deferredDir +  /* ):
    PartList = csv.reader(open(filename))
    BigList.extend(PartList)

Is that what you had in mind?

问题回答

First, the last line in the store function needs to be like this f.close()

Your store function saves the values in a newline-separated manner. To read all the files, should be enough:

def getDeferredRecords():
    """docstring for getDeferredRecords"""
    return dict((infile, list(iter(file(infile)))) 
                     for infile in glob.glob(deferredDir +  /* ))

Explanation: a file is iterable, so you can do for line in file: print line for example. With list(iter(file)) you have the lines of a file in a list. dict((a, b) for a, b in foo) returns a dictionary with {a: b} pairs. The return value of the function is a dictionary with the format {filename: list_of_lines_in_file}. Keep in mind that the list elements are strings with a trailing newline.

The Python cvs module is likely a good answer:
http://docs.python.org/library/csv.html

Question:

glob.glob() returns an iterable already, so I do not see the point here...

[infile for infile in glob.glob(deferredDir +  /* )]

Rather:

BigList = []
for filename in glob.glob(deferredDir +  /* ):
    #CVS read code here
    #add to BigList

Food for thought.

Incorporating ideas from Tim Pietzcker, here are the re-written functions:

def storeDeferredRecords(records):
    """docstring for createFile"""
    now = datetime.datetime.now()
    filename = deferredDir +  /  + now.strftime("%Y%m%d-%H%M%S")
    f = csv.writer(open(filename,  w ), delimiter= , )
    f.writerows(records)

def getDeferredRecords():
    """docstring for getDeferredRecords"""
    for filename in glob.glob(deferredDir +  /* ):
        def_records = csv.reader(open(filename, r ))
        records.extend(def_records)

I used csv.writer instead of using the previous code block:

f = open(filename,  w )
newlist = map(lambda(x): str(x)[1:-1], records)
for item in newlist:
        f.write("%s
" % item)
f.close

Thanks to all those who replied!





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

热门标签