print()函数输出编码报错
python3吧
全部回复
仅看楼主
level 1
PMHI💯 楼主
今天刚学python3 听说是unicode编码 就试着输出了一下十二星座 如下:
>>> for i in range(0,12):
print(chr(9800+i),end="->")
♈->♉->♊->♋->♌->♍->♎->♏->♐->♑->♒->♓->
完全OK 莫得问题
然后我就想输出到文件里面看下是什么样的,结果报了一个编码错误,代码和报错如下,请问大家这是为什么啊?
>>> f = open('D:/printtext.txt','w+')
>>> for i in range(0,12):
print(chr(9800+i),end="->",file=f,flush=True)
Traceback (most recent call last):
File "<pyshell#47>", line 2, in <module>
print(chr(9800+i),end="->",file=f,flush=True)
UnicodeEncodeError: 'gbk' codec can't encode character '\u2648' in position 0: illegal multibyte sequence
2020年03月08日 18点03分 1
level 1
PMHI💯 楼主
直接在IDLE上打的
2020年03月08日 18点03分 2
level 1
PMHI💯 楼主
这么晚 有没有人帮忙看看 谢谢啦
2020年03月08日 18点03分 3
level 1
应该是由于编码的问题,GBK不行
2020年07月03日 06点07分 4
level 1
用utf8编码,在顶部申明用utf8编码
2020年07月04日 04点07分 5
level 1
f = open('D:/printtext.txt','w+',encoding='UTF-8')
for i in range(0,12):
print(chr(9800+i),end="->",file=f,flush=True)
2020年08月27日 03点08分 7
1