473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Novice: replacing strings with unicode variables in a list

Hi,

Im totally new to Python so please bare with me.

Data is entered into my program using the folling code -

str = raw_input(command)
words = str.split()

for word in words:
word = unicode(word,'latin-1')
word.encode('utf8')

This gives an error:

File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x94' in
position 0
: character maps to <undefined>

but the following works.

str = raw_input(command)
words = str.split()

for word in words:
uni = u""
uni = unicode(word,'latin-1')
uni.encode('utf8')

so the problem is that I want replace my list with unicode variables.
Or maybe I should create a new list.

I also tried this:

for word in words[:]:
word = u""
word = unicode(word,'latin-1')
word.encode('utf8')
print word

but got TypeError: decoding Unicode is not supported.

What should I be doing?

Thanks for your help,

Aine.

Dec 6 '06 #1
7 3490
ai********@yahoo.com wrote:
Hi,

Im totally new to Python so please bare with me.

Data is entered into my program using the folling code -

str = raw_input(command)
words = str.split()

for word in words:
word = unicode(word,'latin-1')
word.encode('utf8')

This gives an error:

File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x94' in
position 0
: character maps to <undefined>

but the following works.

str = raw_input(command)
words = str.split()

for word in words:
uni = u""
uni = unicode(word,'latin-1')
uni.encode('utf8')

This is the exact same code as the one above - there is no type declaration
in python, so your

uni = u''

statement is bollocks.

And I seriously doubt, that the above code and the above error message are
related - as you can see, the error is from cp850, a windows codepage. But
that isn't in the code above - so there must be something else happening.

Please provide the full script, and the desired input - then we might be
able to help you.

Diez
Dec 6 '06 #2
Diez B. Roggisch wrote:
Please provide the full script, and the desired input - then we might be
able to help you.
the full *traceback* would pretty useful, too:

http://effbot.org/pyfaq/tutor-i-need...hould-i-do.htm

my guess is that the OP left out the print statement that causing the error, and the
traceback lines that pointed to that print statement:
more test.py
uni = u"p\x95l"
print uni
python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
print uni
File "C:\python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x95' in position 1
: character maps to <undefined>

</F>

Dec 6 '06 #3
ai********@yahoo.com wrote:
Im totally new to Python so please bare with me.
That's no problem, really. I don't use a spellchecker, either, and it
wouldn't have protected you from that particular typo...
Data is entered into my program using the folling code -

str = raw_input(command)
words = str.split()

for word in words:
word = unicode(word,'latin-1')
word.encode('utf8')

This gives an error:

File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x94' in
position 0
: character maps to <undefined>

but the following works.

str = raw_input(command)
words = str.split()

for word in words:
uni = u""
uni = unicode(word,'latin-1')
uni.encode('utf8')
Here you show us the same code twice, as the

uni = u""

assignment has no effect, and a traceback that is probably generated when
you try to

print uni

Here's my guess: The encoding you actually need is cp850, the same that your
Python interpreter is trying to use, but in which unichr(0x94) is
undefined. In general, you are not free to use a random encoding; rather,
you have to use what your console expects.

import sys

s = raw_input(command)
s = unicode(s, sys.stdin.encoding) # trust python to find out the proper
# encoding. If that fails use a constant,
# probably "cp850"
words = s.split():
for word in words:
print word # trust python, but if it doesn't work out:
# word = word.encode("cp850")
# print word

By the way, strings are immutable (cannot be altered once created), so the
following
word.encode('utf8')
print word
is actually spelt

word = word.encode("utf8")
print word

If your data is not read from the console and it contains characters that
cannot be printed, unicode.encode() accepts a second parameter to deal with
it, see
>>help(u"".encode)
Peter
Dec 6 '06 #4
Peter Otten wrote:
words = s.split():
Oops, remove bogus colon here.

Dec 6 '06 #5

ai********@yahoo.com wrote:
Hi,

Im totally new to Python so please bare with me.

Data is entered into my program using the folling code -

str = raw_input(command)
words = str.split()

for word in words:
word = unicode(word,'latin-1')
word.encode('utf8')
The above statement produces a string in utf8 and then throws it away.
It does not update "word". To retain the utf8 string, you would have to
do word = word.encode('utf8') and in any case that won't update the
original list.

*** missing source code line(s) here ***
>
This gives an error:
*** missing traceback lines here ***
File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x94' in
position 0
: character maps to <undefined>

No, it doesn't. You must have put "print word" to get the error that
you did.
*Please* when you are asking a question, copy/paste (1) the exact
source code that you ran (2) the exact traceback that you got.

>
but the following works.
What do you mean by "works"? It may not have triggered an error, but on
the other hand it doesn't do anything useful.
>
str = raw_input(command)
words = str.split()

for word in words:
uni = u""
Above line is pointless. Removing it will have no effect
uni = unicode(word,'latin-1')
uni.encode('utf8')
Same problem as above -- utf8 string is produced and then thrown away.
>
so the problem is that I want replace my list with unicode variables.
Or maybe I should create a new list.

I also tried this:

for word in words[:]:
word = u""
word = unicode(word,'latin-1')
You got the error on the above statement because you are trying
(pointlessly) to decode the value u"". Decoding means to convert from
some encoding to unicode.
word.encode('utf8')
Again, utf8 straight down the gurgler.
print word
This (if executed) will try to print the UNICODE version, and die [as
in the 1st example] encoding the unicode in cp950, which is the
encoding for your Windows command console.
>
but got TypeError: decoding Unicode is not supported.

What should I be doing?
(1) Reading the Unicode howto: http://www.amk.ca/python/howto/

(2) Writing some code like this:

| >>strg = "\x94 foo bar zot"
| >>words = strg.split()
| >>words
| ['\x94', 'foo', 'bar', 'zot']
| >>utf8words = [unicode(word, 'latin1').encode('utf8') for word in
words]
| >>utf8words
| ['\xc2\x94', 'foo', 'bar', 'zot']
| >>>

HTH,
John

Dec 6 '06 #6

Fredrik Lundh skrev:
Diez B. Roggisch wrote:
Please provide the full script, and the desired input - then we might be
able to help you.

the full *traceback* would pretty useful, too:

http://effbot.org/pyfaq/tutor-i-need...hould-i-do.htm

my guess is that the OP left out the print statement that causing the error, and the
traceback lines that pointed to that print statement:
more test.py
uni = u"p\x95l"
print uni
python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
print uni
File "C:\python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x95' in position 1
: character maps to <undefined>

</F>
Thanks for the replies. OK heres the full code I'm using now -

compare = u"Äis"
print compare

str = raw_input("Enter music:")
words = str.split()

uniList=[]
for word in words:
uni=unicode(word,'latin-1')
uniList.append(uni)

print uniList[0]

if(compare!=uniList[0]):
print "Not the same: " + compare + " " + uniList[0]

This gives the following error -

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 14, in <module>
print uniList[0]
File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x8e' in
position 0
: character maps to <undefined>

How come I can print compare but not uniList[0]? What encoding is being
used to store compare? How can I print out the raw bytes stored in
compare and uniList[0]?

Thanks again for your help,

Aine.

Dec 6 '06 #7

aine_ca...@yahoo.com skrev:
Fredrik Lundh skrev:
Diez B. Roggisch wrote:
Please provide the full script, and the desired input - then we mightbe
able to help you.
the full *traceback* would pretty useful, too:

http://effbot.org/pyfaq/tutor-i-need...hould-i-do.htm

my guess is that the OP left out the print statement that causing the error, and the
traceback lines that pointed to that print statement:
more test.py
uni = u"p\x95l"
print uni
python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
print uni
File "C:\python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x95' in position 1
: character maps to <undefined>

</F>

Thanks for the replies. OK heres the full code I'm using now -

compare = u"Äis"
print compare

str = raw_input("Enter music:")
words = str.split()

uniList=[]
for word in words:
uni=unicode(word,'latin-1')
uniList.append(uni)

print uniList[0]

if(compare!=uniList[0]):
print "Not the same: " + compare + " " + uniList[0]

This gives the following error -

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 14, in <module>
print uniList[0]
File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x8e' in
position 0
: character maps to <undefined>

How come I can print compare but not uniList[0]? What encoding is being
used to store compare? How can I print out the raw bytes stored in
compare and uniList[0]?

Thanks again for your help,

Aine.
Cool. It works for me now.

import sys

compare = u"Äis"
print compare

str = raw_input("Enter music:")
words = str.split()

uniList=[]
for word in words:
uni=unicode(word,sys.stdin.encoding)
uniList.append(uni)

print uniList[0]

if(compare!=uniList[0]):
print "Not the same: " + compare + " " + uniList[0]

Dec 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
10
by: Andrew L | last post by:
Hello all, What strategy should I use in solving the following problem? I have a list of unicode strings which I would like to compare with its English language 'equivalent.' eg "reykjavík"...
2
by: Christopher Beltran | last post by:
I am currently trying to replace certain strings, not single characters, with other strings inside a word document which is then sent to a browser as a binary file. Right now, I read in the word...
1
by: Larry Neylon | last post by:
Hi, I'm working on a VBScript application on IIS6 and I'm looking for some advice about the best way of replacing or improving session variable usage. The application is in a secure extranet...
29
by: Ron Garret | last post by:
>>> u'\xbd' u'\xbd' >>> print _ Traceback (most recent call last): File "<stdin>", line 1, in ? UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in position 0: ordinal not in...
0
by: Anthony Baxter | last post by:
SECURITY ADVISORY Buffer overrun in repr() for UCS-4 encoded unicode strings http://www.python.org/news/security/PSF-2006-001/ Advisory ID: PSF-2006-001 Issue Date: October 12, 2006...
2
by: erikcw | last post by:
Hi, I'm parsing xml data with xml.sax and I need to perform some arithmetic on some of the xml attributes. The problem is they are all being "extracted" as unicode strings, so whenever I try to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.