English 中文(简体)
处理多语文名录(Python)
原标题:Dealing with multi-language directories (Python)

我试图开一个档案,我刚刚认识到,我与我的用户名(俄罗斯语)有麻烦。 关于如何适当校正/编码从而实现闲 to的建议?

I m, p. 2.6.5

xmlfile = open(u"D:\Users\Эрик\Downloads\temp.xml", "r")

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    xmlfile = open(str(u"D:\Users\Эрик\Downloads\temp.xml"), "r")
UnicodeEncodeError:  ascii  codec can t encode characters in position 9-12: ordinal not in range(128)

os.sys.getfilesystemencoding() mbcs

xmlfile = open(u”D:UsersЭ Zuloudownloads emp.xml.encode(“mbcs”),“r”

Traceback (most recent call last): File "", line 1, in xmlfile = open(u”D:UsersЭ Zuloudownloads emp.xml.encode(“mbcs”),“r” IOError: [Errno 22] invalid mode ( r ) or filename: D:UsersY?eeDownloads emp.xml

问题回答

第一个问题是,除非你使用<代码>r”raw quote"。 在2.6.5中,你需要专门处理你统法协会的编码,但你可能需要在你的源代码中提出文件编码,如:

# -*- coding: utf-8 -*-

http://www.python.org/dev/peps/pep-0263/“rel=“nofollow noreferer”>。 它是互动协作的一个例子:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2
>>> f = r"D:UsersЭрикDownloads	emp.xml"
>>> f
 D:\Users\xd0xadxd1x80xd0xb8xd0xba\Downloads\temp.xml 
>>> x = open(f,  w )
>>> x.close()
>>> 
$ ls D*
D:UsersЭрикDownloads	emp.xml

是的,这属于一个特殊系统,因此,<条码>><>>> 代码> 具有实际意义,我的终端编码为utf-8,但运作良好。 在阅读档案时,你可能只得向牧师提供 co。

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"





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签