[Tutor] Parsing data from a set of files iteratively

Steven D'Aprano steve at pearwood.info
Sun May 27 18:47:04 CEST 2012


Spyros Charonis wrote:

> The problem is that the list (z_coords1) returns as an empty list. I know
> the code works (too large to post here)

So you want us to diagnose a problem in code that we can't see? I admire your 
confidence in our powers of deduction.


[...]
> Short of some intricacy with the scopes of the program I may be missing, I
> am not sure why this is happening? Deos anybody have
> any ideas? Many thanks for your time.

Your code's indentation is messed up again, which makes it impossible to guess 
what it actually does.

Also, it is hard to understand what it *should* do. I'm not sure why you are 
writing data to a pickle file, only to read it back in again. You already have 
the data in memory, why not just keep it there?

Also, your function serialize_pipeline_model is a misleading name. "Serialize" 
in computer science and programming means to take a data structure and turn it 
into a format suitable for saving to a file. You are doing the opposite, 
taking a file and reading it into a data structure.

Here is my best guess as to what you might need:


def get_zcoords(filename):
     """Read z coordinate data from the named file, and return it as a list."""
     f = open(filename, 'r')
     zcoords = []
     # ... stuff goes here that you don't show
     # ... I guess you read from the file and process it somehow
     # ...
     f.close()
     charged_groups = (lys_charged_group + arg_charged_group
                       + his_charged_group + asp_charged_group
                       + glu_charged_group
                       )
     for group in charged_groups:
         zcoords.append(float(group[48:54]))
     return zcoords


location = '/Users/spyros/Desktop/3NY8MODELSHUMAN/HomologyModels/'
zdata = []
for filename in os.listdir(location):
     zdata.extend(get_zcoords(filename))

print 'Z-VALUES FOR ALL CHARGED RESIDUES'
print zdata


If you need to keep those z coordinates for later use, you can pickle them, 
once, at the end of the process, and then later unpickle them.


import pickle
f = open("z_coords1.dat", "wb")
pickle.dump(zdata, f)
f.close()


f = open("z_coords1.dat", "rb")
zdata2 = pickle.load(f)
f.close()

assert zdata == zdata2, "error in pickle/unpickle round trip!"



Does this help?



-- 
Steven


More information about the Tutor mailing list