【发布时间】:2012-03-26 11:33:45
【问题描述】:
- 我有 1 个带有 utf-8 字符(名称)的源文件
- 我有 1 个具有相同字符编码的输出文件。
- 我正在处理一个 html 页面,粘贴和剪切有用的 供我归档的信息。
- 我在“friendsNames”txt 文件中使用“éáűúőóüöäđĐ”字符。
我给出了这个错误:
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。此外,您的错误消息和您的代码示例不匹配。 -
如果你想进一步了解unicode,我强烈推荐bit.ly/unipain
标签: python utf-8 character-encoding codec urlopen