免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5334 | 回复: 3
打印 上一主题 下一主题

python unicode 中文输出 求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-24 10:39 |只看该作者 |倒序浏览

  1. # -*- coding:utf-8 -*-

  2. import os
  3. import urllib

  4. def printfile(dir):
  5.         files = [f for f in os.listdir(dir)
  6.                         if os.path.isfile(os.path.join(dir , f))]
  7.         for file in files:
  8.                 name = unicode(file)
  9.                 print name.encode('utf-8')
  10.         dire = [d for d in os.listdir(dir)
  11.                    if os.path.isdir(os.path.join(dir , d))]
  12.         for x in dire:
  13.                 #y = dir + "\\" + x
  14.                 printfile(os.path.join(dir, x))


  15. printfile('e:/book')
复制代码


  1. PerlCookBook.chm
  2. The_Perl_Programming_Language_en.pdf
  3. Traceback (most recent call last):
  4.   File "d:\MyProject\python\MyPython\listDir.py", line 19, in <module>
  5.     printfile('e:/book')
  6.   File "d:\MyProject\python\MyPython\listDir.py", line 10, in printfile
  7.     name = unicode(file)
  8. UnicodeDecodeError: 'ascii' codec can't decode byte 0xa9 in position 3: ordinal not in range(128)
复制代码

看错误信息应该是没有按照utf-8来处理  为什么呢 呵呵

论坛徽章:
0
2 [报告]
发表于 2007-04-24 13:59 |只看该作者
好吧  我在csdn上问到了 呵呵
name = unicode(file.'cp936')

初学期间 低级问题可能比较多 大家请见谅

论坛徽章:
0
3 [报告]
发表于 2007-04-26 10:19 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2007-04-27 21:11 |只看该作者
原帖由 DraculaW 于 2007-4-24 13:59 发表
好吧  我在csdn上问到了 呵呵
name = unicode(file.'cp936')

初学期间 低级问题可能比较多 大家请见谅


name = unicode(file.'gb2312')
行吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP