[Tutor] pickle.dump yielding awkward output
Dave Angel
davea at davea.name
Mon Feb 4 00:07:15 CET 2013
On 02/03/2013 02:26 PM, Spyros Charonis wrote:
> Hello Pythoners,
>
> I am experiencing a strange result with the pickle module when using it to
> write certain results to a separate file.
>
> In short, I have a program that reads a file, finds lines which satisfy
> some criteria, and extracts those lines, storing them in a list. I am
> trying to write this list to a separate file.
>
> The list of extracted lines looks like this:
>
> ATOM 1 N GLN A 1 29.872 13.384 54.754 1.00 60.40
> N
>
> ATOM 2 CA GLN A 1 29.809 11.972 54.274 1.00 58.51
> C
>
> ATOM 3 C GLN A 1 28.376 11.536 54.029 1.00 55.13
> C
>
> The output stored from the call to the pickle.dump method, however, looks
> like this:
>
> (lp0
> S'ATOM 1 N GLN A 1 29.872 13.384 54.754 1.00 60.40
> N \r\n'
> p1
> aS'ATOM 2 CA GLN A 1 29.809 11.972 54.274 1.00 58.51
> C \r\n'
> p2
> aS'ATOM 3 C GLN A 1 28.376 11.536 54.029 1.00 55.13
> C \r\n'
>
> The code I am using to write the output to an external file goes as follows:
>
> def export_antibody_chains():
> ''' EXPORT LIST OF EXTRACTED CHAINS TO FILE '''
> chains_file = open(query + '_Chains', 'wb')
> pickle.dump(ab_chains, chains_file) # ab_chains is global
> chains_file.close()
> return
>
> Does anyone know why the strings lp0, S', aS' are showing up?
>
>
Pickle stores the type of each variable, as well as the value, and
stores it in a way as to make it easy to "unpickle" it.
--
DaveA
More information about the Tutor
mailing list