472,805 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 3433
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.