python charmap_unicode - UnicodeEncodeError: 'charmap' codec can't encode character (in Python 3.3)

在尝试打印Python中所有ASCII字符时,遇到'charmap' codec错误,无法编码'x80'字符。问题与Python 3.3的编码设置有关,涉及到ASCII字符集的限制。解决方案可能涉及更改输出编码或处理非ASCII字符。
摘要由CSDN通过智能技术生成

When I'm trying to print all ASCII chars in Python only 127 are getting printed and I'm getting an error with output after 127. I'm unable to understand what does that error mean.

Example:

t = list(range(0, 256))

for x in t:

print(str(x) + ". " + chr(x))

Error:

Traceback (most recent call last):

File "D:\Study\Pedia\Python Book\6. Functions\2. Built-in functions\ord_chr.py", line 6, in

print(str(x) + ". " + str(chr(x)))

File "C:\Python33\lib\encodings\cp1252.py", line 19, in encode

return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\x80' in position 5: character maps to

Note: I can't show you the complete output because I can't copy complete raw output from by sublime output console. Don't know the real reason why it can't get copied.

python

unicode

encode

python-3.3

trackback

|

this question

edited Jan 14 '14 at 15:11 asked Jan 14 '14 at 14:46

Yousuf Memon 1,739 7 24 49      @Wooble Edited. –

Yousuf Memon Jan 14 '14 at 14:52

|

2 Answers

2

---Accepted---Accepted---Accepted---

ASCII includes definitions for 128 characters (0 to 127).

\x80 (128) is not included there.ill it puts up a error like this, I use print(data1) and the html is printed properlyand I am using python 3.5.0 import reimport urllib.requestcity = input("city name") url = "http://www.weather-forecast.com/locations/"+city+"/forecas

|

this answer answered Jan 14 '14 at 14:50

falsetru 189k 24 253 293      But the ASCII chart has 256 chars. Maybe I'm wrong. –

Yousuf Memon Jan 14 '14 at 14:54      @YousufMemon, Which ASCII chart? –

falsetru Jan 14 '14 at 14:56      @YousufMemon: Where'd you find this chart? Whatever it's showing isn't ASCII. –

geoffspear Jan 14 '14 at 14:56      It's extended ASCII:

theasciicode.com.ar/ascii-control-characters/… –

Thorsten Kranz Jan 14 '14 at 14:57 1   Note that there are about 100 different "extended ASCII" encodings which is why this wholly ambiguous phrase should be entirely avoided. The table which was linked in an earlier comment likely represents CP850, which is used by MS-DOS and is almost completely irrelevant nowadays. –

ntoskrnl Jan 14 '14 at 17:42

|

show more comments

When I try you code on Windows, Python 2.7, it works without exception. I had to take screenshots as copying the text simply ignored those extended ascii characters.

|

this answer answered Jan 14 '14 at 15:03

Thorsten Kranz 7,700 17 37

|

tried several of the answers in many a thread but none of them seemed to work properly for my problem. import jsondef parse(fn): results = [] with open(fn) as f: json_obj = json.loads(open(fn).read()) for r in json

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值