FFFF Opening Pickle Files using ma.dump in Python 3 · Issue #5491 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Opening Pickle Files using ma.dump in Python 3 #5491

@JacksonTanBS

Description

@JacksonTanBS

When using numpy.ma.dump to save a masked array in Python 3:

np.ma.dump(a, pathtofile)

I got the following error:

/home/btan1/.virtualenvs/python3.3/lib/python3.3/site-packages/numpy/ma/core.py in dump(a, F)
   7141     if not hasattr(F, 'readline'):
   7142         F = open(F, 'w')
-> 7143     return pickle.dump(a, F)
   7144 
   7145 def dumps(a):

TypeError: must be str, not bytes

This is with NumPy 1.9.1 and Python 3.3.2.

From what I found out, it is because in Python 3 pickle files has to be opened in binary mode, i.e. L7142 should be F = open(F, 'wb'). Indeed, if I were to change it to:

with open(pathtofile, 'wb') as f:
    pickle.dump(a, f)

the error goes away.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0