www.un.org/Depts/DGACM/index_spanish.htm 第一期:
xmlfile = open(u"D:\Users\Эрик\Downloads\temp.xml", "r")
### The above line should be OK, provided that you have the correct coding line
### For example # coding: cp1251
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
xmlfile = open(str(u"D:\Users\Эрик\Downloads\temp.xml"), "r")
### HOWEVER the above traceback line shows you actually using str()
### which is DIRECTLY causing the error because it is attempting
### to decode your filename using the default ASCII codec -- DON T DO THAT.
### Please copy/paste; don t type from memory.
UnicodeEncodeError: ascii codec can t encode characters in position 9-12: ordinal not in range(128)
<>第二个问题:
<>代码>.sys.getfilesystemencoding(/code>
xmlfile = open(u"D:UsersЭрикDownloads emp.xml".encode("mbcs"), "r")
### (a) is interpreted as a TAB character, hence the file name is invalid.
### (b) encoding with mbcs seems not to be useful; it messes up your name ("Y?ee").
Traceback (most recent call last):
File "", line 1, in xmlfile = open(u"D:UsersЭрикDownloads emp.xml".encode("mbcs"), "r")
IOError: [Errno 22] invalid mode ( r ) or filename: D:UsersY?eeDownloads emp.xml
www.un.org/Depts/DGACM/index_spanish.htm 关于Windows硬编码名称的一般建议,按优先排序:
(1) Don t
(2) Use /
e.g. "c:/temp.xml"
(3) Use raw strings with backslashes r"c: emp.xml"
(4) Use doubled backslashes "c:\temp.xml"