2007年05月17日 星期四 16:47
大家好,您好!
请教一个问题!
walk('c:\\ddd', visit, 0)
def visit(arg, dirname, names):
encoding = locale.getdefaultlocale()[1]
print encoding
print dirname
for file in names:
print file
如果遇到了中文的文件名,就有如下错误 :
Traceback (most recent call last):
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button
walk(self.textCtrl1.GetValue(), self.visit, 0)
File "D:\Python25\lib\ntpath.py", line 328, in walk
func(arg, top, names)
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit
print file
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
应该怎么解决呢?
致
礼!
李鹏
jeffskylee在163.com
2007-05-17
2007年05月17日 星期四 16:57
最簡單的方法,不使用中文路徑及中文文件名 BR, Jianhua Zhu 2007-05-17 From: 李鹏 Time: 2007-05-17 16:49:19 To: python-chinese在lists.python.cn Cc: Subject: [python-chinese]?于?取中文路?名的?? 大家好,您好! 请教一个问题! walk('c:\\ddd', visit, 0) def visit(arg, dirname, names): encoding = locale.getdefaultlocale()[1] print encoding print dirname for file in names: print file 如果遇到了中文的文件名,就有如下错误 : Traceback (most recent call last): File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button walk(self.textCtrl1.GetValue(), self.visit, 0) File "D:\Python25\lib\ntpath.py", line 328, in walk func(arg, top, names) File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit print file UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) 应该怎么解决呢? 致 礼! 李鹏 jeffskylee在163.com 2007-05-17 _______________________________________________ python-chinese Post: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20070517/f851a3af/attachment.html
2007年05月17日 星期四 17:04
Zuggie Zhu£¬ÄúºÃ£¡
¡¡¡¡Ê¹ÓÃfile.encode('936')Ö®ºó²Å¿ÉÒÔÕý³£´òÓ¡³öÀ´¡£
µ«ÊÇÕâ¸ö·½·¨Ò²Ì«²»Í¨ÓÃÁË£¬Ã¿¸ö¿ÉÄÜÊÇÖÐÎĵĵط½¶¼ÐèÒªÕâôתÂð£¿ÓÐûÓÐÒ»ÀÍÓÀÒݵĽâ¾ö·½·¨£¿
²»Ê¹ÓÃÖÐÎÄ·¾¶ÓëÖÐÎÄÎļþÃû£¬Õâ¸ö²»Ì«ÏÖʵ¡£ÎҵijÌÐò¿ÉÒÔ²»Ó㬵«ÊDz»Äܱ£Ö¤Ó²ÅÌÉϵÄÎļþ¶¼ÊÇÓ¢Îİ¡¡£
======== 2007-05-17 16:58:24 ÄúÔÚÀ´ÐÅÖÐдµÀ£º ========
×îºÎµÄ·½·¨£¬²»Ê¹ÓÃÖÐÎÄ·½¼°ÖÐÎÄÎļþÃû
BR,
Jianhua Zhu
2007-05-17
From: ÀîÅô
Time: 2007-05-17 16:49:19
To: python-chinese在lists.python.cn
Cc:
Subject: [python-chinese]?ÓÚ?È¡ÖÐÎÄ·?ÃûµÄ??
´ó¼ÒºÃ£¬ÄúºÃ£¡
Çë½ÌÒ»¸öÎÊÌâ!
walk('c:\\ddd', visit, 0)
def visit(arg, dirname, names):
encoding = locale.getdefaultlocale()[1]
print encoding
print dirname
for file in names:
print file
Èç¹ûÓöµ½ÁËÖÐÎĵÄÎļþÃû£¬¾ÍÓÐÈçÏ´íÎó £º
Traceback (most recent call last):
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button
walk(self.textCtrl1.GetValue(), self.visit, 0)
File "D:\Python25\lib\ntpath.py", line 328, in walk
func(arg, top, names)
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit
print file
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
Ó¦¸ÃÔõô½â¾öÄØ£¿
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÖÂ
Àñ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÀîÅô
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡jeffskylee在163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2007-05-17
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = = = =
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÖÂ
Àñ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÀîÅô
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡jeffskylee在163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2007-05-17
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070517/2019ff58/attachment.htm
2007年05月17日 星期四 17:43
encoding = sys.getfilesystemencoding() print unicode(filename,encoding) ÊÔÊÔÕâÑù¿ÉÒÔÂð? ÔÚ07-5-17£¬ÀîÅô <jeffskylee在163.com> дµÀ£º > > Zuggie Zhu£¬ÄúºÃ£¡ > ʹÓÃfile.encode('936')Ö®ºó²Å¿ÉÒÔÕý³£´òÓ¡³öÀ´¡£ > > µ«ÊÇÕâ¸ö·½·¨Ò²Ì«²»Í¨ÓÃÁË£¬Ã¿¸ö¿ÉÄÜÊÇÖÐÎĵĵط½¶¼ÐèÒªÕâôתÂð£¿ÓÐûÓÐÒ»ÀÍÓÀÒݵĽâ¾ö·½·¨£¿ > > ²»Ê¹ÓÃÖÐÎÄ·¾¶ÓëÖÐÎÄÎļþÃû£¬Õâ¸ö²»Ì«ÏÖʵ¡£ÎҵijÌÐò¿ÉÒÔ²»Ó㬵«ÊDz»Äܱ£Ö¤Ó²ÅÌÉϵÄÎļþ¶¼ÊÇÓ¢Îİ¡¡£ > > ======== 2007-05-17 16:58:24 ÄúÔÚÀ´ÐÅÖÐдµÀ£º ======== > > > ×îºÎµÄ·½·¨£¬²»Ê¹ÓÃÖÐÎÄ·½¼°ÖÐÎÄÎļþÃû > > > > BR, > > Jianhua Zhu > 2007-05-17 > ------------------------------ > *From: * ÀîÅô > *Time: * 2007-05-17 16:49:19 > *To: * python-chinese在lists.python.cn > *Cc: * > *Subject: * [python-chinese]?ÓÚ?È¡ÖÐÎÄ·?ÃûµÄ?? > > ´ó¼ÒºÃ£¬ÄúºÃ£¡ > > Çë½ÌÒ»¸öÎÊÌâ! > > walk('c:\\ddd', visit, 0) > > def visit(arg, dirname, names): > encoding = locale.getdefaultlocale()[1] > print encoding > print dirname > for file in names: > print file > > Èç¹ûÓöµ½ÁËÖÐÎĵÄÎļþÃû£¬¾ÍÓÐÈçÏ´íÎó £º > > Traceback (most recent call last): > > File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button > walk(self.textCtrl1.GetValue(), self.visit, 0) > File "D:\Python25\lib\ntpath.py", line 328, in walk > func(arg, top, names) > File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit > print file > > UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) > > Ó¦¸ÃÔõô½â¾öÄØ£¿ > > Ö > Àñ£¡ > > ÀîÅô > jeffskylee在163.com > 2007-05-17 > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > = = = = = = = = = = = = = = = = = = = = = = > > Ö > Àñ£¡ > > ÀîÅô > jeffskylee在163.com > 2007-05-17 > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070517/acccc2d1/attachment.htm
2007年05月17日 星期四 17:57
Í·Ì«ÔΣ¬ÄúºÃ£¡
¡¡¡¡²»ÐС£¡£¡£
mbcs
Traceback (most recent call last):
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button
walk(self.textCtrl1.GetValue(), self.visit, 0)
File "D:\Python25\lib\ntpath.py", line 328, in walk
func(arg, top, names)
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 65, in visit
print unicode(filename,encoding)
TypeError: decoding Unicode is not supported
ÎÒÔÚÎļþÍ·²¿Ð´ÁË
#-*- coding: cp936 -*-
mbcs ÊÇ´òÓ¡³öµÄsys.getfilesystemencoding()
======== 2007-05-17 17:43:18 ÄúÔÚÀ´ÐÅÖÐдµÀ£º ========
encoding = sys.getfilesystemencoding()
print unicode(filename,encoding)
ÊÔÊÔÕâÑù¿ÉÒÔÂð?
ÔÚ07-5-17£¬ÀîÅô <jeffskylee在163.com > дµÀ£º
Zuggie Zhu£¬ÄúºÃ£¡
¡¡¡¡Ê¹ÓÃfile.encode('936')Ö®ºó²Å¿ÉÒÔÕý³£´òÓ¡³öÀ´¡£
µ«ÊÇÕâ¸ö·½·¨Ò²Ì«²»Í¨ÓÃÁË£¬Ã¿¸ö¿ÉÄÜÊÇÖÐÎĵĵط½¶¼ÐèÒªÕâôתÂð£¿ÓÐûÓÐÒ»ÀÍÓÀÒݵĽâ¾ö·½·¨£¿
²»Ê¹ÓÃÖÐÎÄ·¾¶ÓëÖÐÎÄÎļþÃû£¬Õâ¸ö²»Ì«ÏÖʵ¡£ÎҵijÌÐò¿ÉÒÔ²»Ó㬵«ÊDz»Äܱ£Ö¤Ó²ÅÌÉϵÄÎļþ¶¼ÊÇÓ¢Îİ¡¡£
======== 2007-05-17 16:58:24 ÄúÔÚÀ´ÐÅÖÐдµÀ£º ========
×îºÎµÄ·½·¨£¬²»Ê¹ÓÃÖÐÎÄ·½¼°ÖÐÎÄÎļþÃû
BR,
Jianhua Zhu
2007-05-17
From: ÀîÅô
Time: 2007-05-17 16:49:19
To: python-chinese在lists.python.cn
Cc:
Subject: [python-chinese]?ÓÚ?È¡ÖÐÎÄ·?ÃûµÄ??
´ó¼ÒºÃ£¬ÄúºÃ£¡
Çë½ÌÒ»¸öÎÊÌâ!
walk('c:\\ddd', visit, 0)
def visit(arg, dirname, names):
encoding = locale.getdefaultlocale()[1]
print encoding
print dirname
for file in names:
print file
Èç¹ûÓöµ½ÁËÖÐÎĵÄÎļþÃû£¬¾ÍÓÐÈçÏ´íÎó £º
Traceback (most recent call last):
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button
walk(self.textCtrl1.GetValue(), self.visit, 0)
File "D:\Python25\lib\ntpath.py", line 328, in walk
func(arg, top, names)
File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit
print file
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
Ó¦¸ÃÔõô½â¾öÄØ£¿
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÖÂ
Àñ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÀîÅô
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡jeffskylee在163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2007-05-17
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = = = =
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÖÂ
Àñ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÀîÅô
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡jeffskylee在163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2007-05-17
_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = = = =
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÖÂ
Àñ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ÀîÅô
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡jeffskylee在163.com
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2007-05-17
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070517/a05265f8/attachment.htm
2007年05月17日 星期四 20:14
On 5/17/07, ÀîÅô <jeffskylee在163.com> wrote: > > Í·Ì«ÔΣ¬ÄúºÃ£¡ > ²»ÐС£¡£¡£ > > mbcs > Traceback (most recent call last): > File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in > OnButton2Button > walk(self.textCtrl1.GetValue(), self.visit, 0) > File "D:\Python25\lib\ntpath.py", line 328, in walk > func(arg, top, names) > File "D:\python_workplace\GotoFile\src\Dialog1.py", line 65, in visit > print unicode(filename,encoding) > TypeError: decoding Unicode is not supported > > ÎÒÔÚÎļþÍ·²¿Ð´ÁË > #-*- coding: cp936 -*- > mbcs ÊÇ´òÓ¡³öµÄsys.getfilesystemencoding() > > python¿ÉÒÔ°´unicodeÀ´Êä³öĿ¼Ãû£¬´òӡʱÈç¹ûÊÇÔÚÃüÁîÐÐÏ£¬python»á×Ô¶¯½øÐÐת»»µÄ£¬ËùÒÔÒ»°ãҪתΪÓë±ê×¼Êä³öÏàͬ±àÂëµÄ×Ö·û´®ÔÙ½øÐÐÊäÈë¡£ -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070517/007cd550/attachment.html
2007年05月17日 星期四 21:15
看来没有正确设置default encoding, 你可以修改default encoding 'ascii' -> 'cp936' 。
在你的脚本的目录下放一个sitecustomize.py:
import sys
sys.setdefaultencoding('cp936')
或者放到Python24\lib里.
vcc
_
----- Original Message -----
From: "李鹏" <jeffskylee at 163.com>
To: <python-chinese at lists.python.cn>
Sent: Thursday, May 17, 2007 4:47 PM
Subject: [python-chinese] 关于读取中文路径名的错误
> 大家好,您好!
>
> 请教一个问题!
>
> walk('c:\\ddd', visit, 0)
>
> def visit(arg, dirname, names):
> encoding = locale.getdefaultlocale()[1]
> print encoding
> print dirname
> for file in names:
> print file
>
> 如果遇到了中文的文件名,就有如下错误 :
>
> Traceback (most recent call last):
> File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button
> walk(self.textCtrl1.GetValue(), self.visit, 0)
> File "D:\Python25\lib\ntpath.py", line 328, in walk
> func(arg, top, names)
> File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit
> print file
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
>
> 应该怎么解决呢?
>
>
> 致
> 礼!
>
>
> 李鹏
> jeffskylee at 163.com
> 2007-05-17
2007年05月17日 星期四 21:28
On 5/17/07, 李鹏 <jeffskylee在163.com> wrote: > 大家好,您好! > > 请教一个问题! > > walk('c:\\ddd', visit, 0) > > def visit(arg, dirname, names): > encoding = locale.getdefaultlocale()[1] > print encoding > print dirname > for file in names: > print file > > 如果遇到了中文的文件名,就有如下错误 : > > Traceback (most recent call last): > File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button > walk(self.textCtrl1.GetValue(), self.visit, 0) > File "D:\Python25\lib\ntpath.py", line 328, in walk > func(arg, top, names) > File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit > print file > UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128) > > 应该怎么解决呢? > 最好程序中统一使用unicode。这样就不会有编码的问题了。 你试试walk函数的第一个参数传个unicode进去:walk(ur'c:\ddd', visit, 0) -- Best Regards, Leo Jay
2007年05月17日 星期四 21:44
vcc,您好!
这个方法应该就是一劳永逸的方法了,多谢
======= 2007-05-17 21:16:01 您在来信中写道:=======
>看来没有正确设置default encoding, 你可以修改default encoding 'ascii' -> 'cp936' 。
>在你的脚本的目录下放一个sitecustomize.py:
>import sys
>sys.setdefaultencoding('cp936')
>
>或者放到Python24\lib里.
>
>vcc
>_
>
>----- Original Message -----
>From: "李鹏" <jeffskylee在163.com>
>To: <python-chinese在lists.python.cn>
>Sent: Thursday, May 17, 2007 4:47 PM
>Subject: [python-chinese] 关于读取中文路径名的错误
>
>
>> 大家好,您好!
>>
>> 请教一个问题!
>>
>> walk('c:\\ddd', visit, 0)
>>
>> def visit(arg, dirname, names):
>> encoding = locale.getdefaultlocale()[1]
>> print encoding
>> print dirname
>> for file in names:
>> print file
>>
>> 如果遇到了中文的文件名,就有如下错误 :
>>
>> Traceback (most recent call last):
>> File "D:\python_workplace\GotoFile\src\Dialog1.py", line 58, in OnButton2Button
>> walk(self.textCtrl1.GetValue(), self.visit, 0)
>> File "D:\Python25\lib\ntpath.py", line 328, in walk
>> func(arg, top, names)
>> File "D:\python_workplace\GotoFile\src\Dialog1.py", line 66, in visit
>> print file
>> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
>>
>> 应该怎么解决呢?
>>
>>
>> 致
>> 礼!
>>
>>
>> 李鹏
>> jeffskylee在163.com
>> 2007-05-17
>_______________________________________________
>python-chinese
>Post: send python-chinese在lists.python.cn
>Subscribe: send subscribe to python-chinese-request在lists.python.cn
>Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = =
致
礼!
李鹏
jeffskylee在163.com
2007-05-17
Zeuux © 2025
京ICP备05028076号