The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
sandefan (39) [Avatar] Offline
#1
Hi,

from Hello World I tried to modify listings 22.6 and 22.7 to accomodate for umlauts. I managed to get no error messages, but the output has still coded umlauts:
['xc3xa4rger', 73, 'heixc3x9f', 8.1987599999999997e-12]

However it should read
['ärger', 73, 'heiß', 8.1987599999999997e-12]

How can I get the program to decode?

What did I do wrong?

This is the program:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# according to http://www.python.org/dev/peps/pep-0263/
# pickle_files.py
# 10-nov-2010, from hello world
# listings 22.6, 22.7
#
import codecs
import pickle
#
# pickle file
my_list = ['ärger', 73, 'heiß', 81.9876e-13]
#
pickle_file = codecs.open('my_pickled_list.pkl', 'w')
pickle.dump(my_list, pickle_file)
pickle_file.close()
#
# unpickle file
pickle_file = codecs.open('my_pickled_list.pkl', 'r')
recovered_list = pickle.load(pickle_file)
pickle_file.close()
#
print recovered_list
#
raw_input("hit ENTER to finish")

Thanks,

Siggi
sandefan (39) [Avatar] Offline
#2
Re: Help with pickle and umlauts, please
This is the beautiful output after a slight modification (with a little help from www.python-forum.de/):

ärger 73 heiß 8.19876e-12

And this is the new code:

# -*- coding: utf-8 -*-
import pickle
#
# pickle file
my_list = ['ärger', 73, 'heiß', 81.9876e-13]
#
pickle_file = open('my_pickled_list.pkl', 'w')
pickle.dump(my_list, pickle_file)
pickle_file.close()
#
# unpickle file
pickle_file = open('my_pickled_list.pkl', 'r')
recovered_list = pickle.load(pickle_file)
pickle_file.close()
#
for x in recovered_list:
........print x,
print
warren.sande (211) [Avatar] Offline
#3
Re: Help with pickle and umlauts, please
Glad you found a solution!

So, it looks like you don't have to do anything special. It appears the pickle module already knows how to handle UTF-8.


Warren Sande
sandefan (39) [Avatar] Offline
#4
Re: Help with pickle and umlauts, please
This was the special:
instead of

print recovered_list

use

for x in recovered_list:
........print x,
print

Just found that with Python3 this pickling business with umlauts is much simpler, no
# -*- coding: utf-8 -*-
and no special tricks to see the original list before pickling and to print the recovered list.

Too bad, Pythoncard and the old Pygame are not compatible with Python3 smilie

However, your book revived my interest into Python quite a lot. As an occasional hobby programmer, I find your "Word Boxe"s, "What's going on", "Thinking like a programmer" and your whole text very instructive, entertaining and easily understandable. I guess, this is due quite a lot to Carter smilie

Siggi

Message was edited by:
sandefan
sandefan (39) [Avatar] Offline
#5
Re: Help with pickle and umlauts, please
I had run the previous codes with Linux-Ubuntu; they work as described above. Howvever, just noticed that "ugly" text appears when done in Windows XP smilie