欢迎使用优快云-markdown编辑器

本文介绍Python环境下处理中文路径的方法及编码格式的选择。针对不同版本的Python,讲解如何正确读取和输出中文目录,并通过实例演示如何避免因编码问题导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python读取中文目录

  • 常用编码格式:ascii、gbk、unicode、utf-8
  • python2.x内部编码默认为unicode

通常写代码时使用utf-8编码,在文件的开头处加入#coding=utf-8,之后在代码定义字符串时编码就是utf-8,比如:
code:

#coding=utf-8
import chardet
a='hello'
b='hello你好'
print chardet.detect(a)
print chardet.detect(b)

result:

{'confidence': 1.0, 'encoding': 'ascii'}
{'confidence': 0.7525, 'encoding': 'utf-8'}`
  • 当字符串是ascii的子集时,则使用utf-8或者说ascii编码(这与utf-8的变长编码有关),此时检测出的编码方式是ascii,实际上认为是utf-8编码即可,使用utf-8解码可以得到正确的结果
  • 当字符串超出ascii的子集,则就能检测出是utf-8编码

python要输出中文必须是gbk编码,不能是utf-8编码,打印utf-8编码的中文会乱码,比如:
code

#coding=utf-8
a='hello你好'
print a.decode('utf-8').encode('gbk')
print a

result

hello你好
hello浣犲ソ

python读取中文目录返回的结果是unicode编码(u’xxx’),要转成gbk编码输出
code

#coding=gbk
import os
import chardet
path = 'h:\影音'
files = os.listdir(unicode(path,'gbk'))
print files
for item in files:
    print item.encode('gbk')

result

[u'\u6b4c\u66f2', u'\u7535\u5f71', u'\u7535\u89c6\u5267', u'\u7efc\u827a']
歌曲
电影
电视剧
综艺

python访问中文目录要使用unicode编码,前面说过了python内部默认使用unicode编码,因此需要转换目录的编码(unicode和utf-8编码是不同的)
code

#coding=utf-8
import os
path = 'h:\影音'
files = os.listdir(path)

result

Traceback (most recent call last):
  File "H:/Program/Projects/Itchat/test1/WindowsPathTest.py", line 6, in <module>
    files = os.listdir(path)
WindowsError: [Error 3] : 'h:\\\xe5\xbd\xb1\xe9\x9f\xb3/*.*'

改正

#coding=utf-8
import os
path = 'h:\影音'
files = os.listdir(unicode(path,'utf-8'))

windows下默认使用gbk编码,因此可以直接使用gbk作为默认的编码,可直接输出中文
code

#coding=gbk
import os
path = 'h:\影音'
files = os.listdir(unicode(path,'gbk'))
for item in files:
    print item.encode('gbk')
print '你好' 

result

歌曲
电影
电视剧
综艺
你好

chardet模块可以用于检测字符串的编码,但是无法检测unicode编码,因为unicode编码前面有一个u(格式:u’xxx’)
code

#coding=utf-8
import chardet
a='hello你好'
b=a.decode('utf-8').encode('gbk')
c=a.decode('utf-8')
print chardet.detect(a)["encoding"]
print chardet.detect(b)["encoding"]
print chardet.detect(c)["encoding"]

result

utf-8
ISO-8859-2

Traceback (most recent call last):
  File "H:/Program/Projects/Itchat/test1/WindowsPathTest.py", line 18, in <module>
    print chardet.detect(c)["encoding"]
  File "D:\PythonAll\Python27\lib\site-packages\chardet\__init__.py", line 25, in detect
    raise ValueError('Expected a bytes object, not a unicode object')
ValueError: Expected a bytes object, not a unicode object
  • 检测结果中把gbk检测成了ISO8859-2,这是一个补充了东欧语言的Latin-2,但是按照gbk解码还是可以得到正确的结果
  • unicode编码检测出现了错误,原因就是unicode编码字符串前面有一个u
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值