- 我有1个源文件与
- 我有1不与相同的字符编码文件UTF-8字符(名称)。
- 我正在处理一个html页面,粘贴并剪切了有用的 信息让我出来文件。
- 我用“éáűúőóüöäđĐ”字在我的 “friendsNames” txt文件
而我给了这个错误:??的Python - 字符编码和解码的问题
Traceback (most recent call last):
File "C:\Users\Rendszergazda\workspace\achievements\hiba.py", line 9, in <module>
s = str(urlopen("http://eu.battle.net/wow/en/character/arathor/"+str(names[0])+"/achievement").read(), encoding='utf-8')
File "C:\Python27\lib\encodings\cp1250.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\ufeff' in position 0: character maps to <undefined>
你觉得什么是我的问题
from urllib import urlopen
import codecs
result = codecs.open("C:\Users\Desktop\Achievements\Result.txt", "a", "utf-8")
fh = codecs.open("C:\Users\Desktop\Achievements\FriendsNames.txt", "r", "utf-8")
line = fh.readline()
names = line.split(" ")
fh.close()
s = urlopen("http://eu.battle.net/wow/en/character/arathor/"+str(names[0])+"/achievement").read(), encoding='utf8')
result.write(str(s))
result.close()
仅供参考:字符“0xfeff”是[BOM](http://en.wikipedia.org/wiki/Byte_order_mark)。此外,您的错误消息和您的代码示例不匹配。 – hochl 2012-03-26 11:45:18
如果你想了解更多关于unicode的信息,我强烈建议http://bit.ly/unipain – 2012-03-26 11:46:35