Python encode utf? iso-8859-1? - python

I am trying to print an element in terminal, with:
print y['value'].decode('iso-8859-1').encode('utf8')
but the text keeps printing all messed up, as in:
LATICÍNIOS LACTVIT LIMITADA
or
PETR├â┬ôPOLIS (which should read PETRÓPOLIS)
what am I doing wrong and how do you suggest I fix it? Thanks!
If I just do:
print i['value']
I will get:
File "C:\Python27\ArcGIS10.1\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xd3' in position 4
: character maps to

Related

What do I need to do to write a string containing card symbol characters to a file using Python

I have this simple code:
f= open("onemajor.txt", "w")
f.write(str(deal))
f.write ("\n")
f.close()
That generates this error:
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2660'
in position 0: character maps to <undefined>
The string being written looks like this:
♠AKJ63♡A3♢AK87♣92 ♠Q972♡9642♢T6♣KQT ♠♡KQ75♢J52♣J87543 ♠T854♡JT8♢Q943♣A6
Can someone tell me what I need to do?

UnicodeEncodeError: 'latin-1' when reading a textfile

I am new to reading textfiles. i run the following code:
with open('sometext.txt', 'rb') as xy: txt = xy.read().decode('utf-8')
i get this error:
UnicodeEncodeError: 'latin-1' codec can't encode character '\u201e' in position 137: ordinal not in range(256)
i already tried to play around with encoding and decoding. but without success. the text in the file is german, may the error depends on that. thanks for help

Error while importing Wikipedia using pip

I'm using Python 2.7 and trying to work with the following code
import wikipedia
input = raw_input("Question: ")
print wikipedia.summary(input)
I see this error when the code is run:
Traceback (most recent call last): File "wik.py", line 5, in
print wikipedia.summary(input) File "C:\Anaconda2\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013'
in position 38: character maps to undefined
How can I fix this? Thanks in advance.
Python 2 defaults to ASCII, which only maps characters between \u0000 and \u007F1. You need to use a different encoding in order to properly get this character (\u2013 is a long dash) and many others outside of ASCII.
Using UTF-8 should work for you, and I believe this print statement will properly output text:
print wikipedia.summary(input).encode("utf8")
For more information on this, check this similar question: UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 3 2: ordinal not in range(128).

UnicodeEncodeError: 'charmap' codec can't encode character character maps to <undefined>

I have a problem with writing to file in unicode. I am using python 2.7.3. It gives me such an error:
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2019' in position 1006: character maps to <undefined>
Here is a sample of my code: error is on line: f3.write(text)
f = codecs.open("PopupMessages.strings", encoding='utf-16')
text = f.read()
print text
f.close()
f3 = codecs.open("3.txt", encoding='utf-16', mode='w')
f3.write(text)
f3.close()
I tried to use 'utf-8' and 'utf-8-sig' also, but it doesn't helped me. I have such symbols in my source file to read: ['\",;?*&$##%] and symbols in different languages.
How can I solve this issue? Please help, I read info on stackoverflow firstly, but it didn't helped me.
delete this line:
print text
and it should work

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128)

I'm trying to print a string from an archived web crawl, but when I do I get this error:
print page['html']
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128)
When I try print unicode(page['html']) I get:
print unicode(page['html'],errors='ignore')
TypeError: decoding Unicode is not supported
Any idea how I can properly code this string, or at least get it to print? Thanks.
You need to encode the unicode you saved to display it, not decode it -- unicode is the unencoded form. You should always specify an encoding, so that your code will be portable. The "usual" pick is utf-8:
print page['html'].encode('utf-8')
If you don't specify an encoding, whether or not it works will depend on what you're printing to -- your editor, OS, terminal program, etc.

Categories

Resources