我已经研究了这个问题,似乎Python 2.7默认使用是ASCII,我不能切换到python 3(默认Unicode),因为库
I have researched about this issue, It seems Python 2.7 default use is ASCII, I cant switch to python 3 (default Unicode) because of libraries
# - * - 编码:utf-8 - * - 打印u'порядке'
似乎打印正常它将是?没有 u 但是: 打印列表(os.walk(ur'c:\somefoler'))返回 \\\и\\\т ... 为什么不可读第一印?另外我使用os.walk与变量我不能使用它与 ur 我只是想了解如何使我的以下代码工作与任何文件夹/文件语言我使用os.walk +保存到文件两者似乎总是不起作用 ???? 其中西里尔文
seems to print fine it will be ?????? without u but: print list(os.walk(ur'c:\somefoler')) returns \u0438\u0442... why not readable as first print? Also I use os.walk with variables I can't use it with ur I'm just trying to understand how could I make my following code work with any folders/files language I use os.walk + save to file both seems not to work always ???? where Cyrillic
def findit(self,root, exclude_files=[], exclude_dirs=[]): exclude_files = (fnmatch.translate(i) for i in exclude_files) exclude_files = '('+')|('.join(exclude_files)+')' exclude_files = repile(exclude_files) exclude_dirs = (os.path.normpath(i) for i in exclude_dirs) exclude_dirs = (os.path.normcase(i) for i in exclude_dirs) exclude_dirs = set(exclude_dirs) for root, dirs, files in os.walk(root): if os.path.normpath(os.path.normcase(root)) in exclude_dirs: # exclude this dir and subdirectories dirs[:] = [] continue for f in files: if not exclude_files.match(os.path.normcase(f)): yield os.path.join(root, f)filelist = list(findit('c:\\',exclude_files = ['* .dll','* .dat','* .log ','* .exe'],exclude_dirs = ['c:/ windows','c:/ program files','c:/ else']))
当它是一个变量,似乎我必须使用 .decode('utf-8')?为什么没有unicode如 u'var'如果它存在,为什么有很多次异常,不可能转换遇到它,并看到很多答案与这样的错误我很难理解它是不是有办法让它工作?
When it's a variable it seems I have to use .decode('utf-8')? Why not unicode such as u'var' if it exists and why there are many times exceptions it's not possible to convert had encountered it and saw a lot of answers with such errors I'm having hard time understanding it isn't there a way to make it just work?
推荐答案尝试
root = ur'c:\somefoler' for current,dirs,files in os.walk(root): for file in files: print file, repr(file)你应该看到正确的事情(在列表中使用的repr旁边)...问题是当您打印列表时,会打印其项目
you should see the proper thing(along side the repr that is used in the list) ... the problem is that when you print a list it prints a repr of its items
更多推荐
Python os.walk使其支持Unicode / UTF
发布评论