Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3431924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:23:10+00:00 2026-05-18T07:23:10+00:00

In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle

  • 0

In Python 2.5 I stored data using this code:

def GLWriter(file_name, string):
   import cPickle
   import zlib
   data = zlib.compress(str(string))
   file = open(file_name, 'w')
   cPickle.dump(data, file)

It worked fine, I was able to read that data by doing that process in reverse. It didn’t need to be secure, just something that wasn’t readable to the human eye. If I put “test” into it and then opened the file it created, it looked like this:

S'x\x9c+I-.\x01\x00\x04]\x01\xc1'
p1
.

For various reasons we’re forced to use Python 3.1 now and we need to code something that can read these data files.

Pickle no longer accepts a string input so I’ve had to open the file with “rb”. When I do that and try opening it with pickle.load(file), I get this error:

File "<stdin>", line 1, in <module>
File "C:\Python31\lib\pickle.py", line 1365, in load
   encoding=encoding, errors=errors).load()
UnicodeDecodingError: 'ascii' codec can't decode byte 0x9c in position 1: ordinal not in range(128)

Figuring that I might not be able to open the file in pickle, I started doing some research and found that pickle is just wrapping a few characters each side of the main block of data that zlib is producing. I then tried to trim it down to zlibs output and put that through zlib.decompress. My issue there is that it reads the file and interprets the likes of “\x04” as four characters rather than one. A lot of testing and searching later and I can’t find a way to make pickle load the file, or make python recognise these codes so I can put it through zlib.

So my question is this:
How can I recover the original data using Python3.1?

I would love to ask my clients to install Python2.5 and do it manually but that’s not possible.

Many thanks for your assistance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:23:11+00:00Added an answer on May 18, 2026 at 7:23 am

    The problem is that Python 3 is attempting to convert the pickled Python 2 string into a str object, when you really need it to be bytes. It does this using the ascii codec, which doesn’t support all 256 8-bit characters, so you are getting an exception.

    You can work around this by using the latin-1 encoding (which supports all 256 characters), and then encoding the string back into bytes:

    s = pickle.load(f, encoding='latin1')
    b = s.encode('latin1')
    print(zlib.decompress(b))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Is there a way to import a Python module stored in a cStringIO data
I'm using Python/Django, but this is more about the data model and how I
Using Python (3.1 or 2.6), I'm trying to read data from binary data files
Is this Python script correct? import urllib, urllib2, cookielib username = 'myuser' password =
Using Python's Networkx library, I created an undirected graph to represent a relationship network
I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar
I am using the App Engine Bulk loader (Python Runtime) to bulk upload entities
If I write a python script using only python standard libraries, using Python 2.6
I'm trying to build a C++ extension for python using swig. I've followed the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.