Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python os.walk是否支持Unicode/UTF-8?_Python_Python 2.7_Unicode_Encoding_Utf 8 - Fatal编程技术网

Python os.walk是否支持Unicode/UTF-8?

Python os.walk是否支持Unicode/UTF-8?,python,python-2.7,unicode,encoding,utf-8,Python,Python 2.7,Unicode,Encoding,Utf 8,我已经研究过这个问题,似乎Python2.7的默认用法是ASCII,我无法切换到Python3(默认Unicode),因为库的原因 #-*-编码:utf-8-*- 打印u'Пцццц' 似乎打印得很好,没有u 但是: 打印列表(os.walk(ur'c:\somefoler'))返回\u0438\u0442…为什么第一次打印时不可读?另外,我使用os.walk和变量,我不能将其与ur一起使用,我只是想了解如何使我的以下代码与任何文件夹/文件一起工作。我使用os.walk+保存到文件这两种语言似乎

我已经研究过这个问题,似乎Python2.7的默认用法是ASCII,我无法切换到Python3(默认Unicode),因为库的原因

#-*-编码:utf-8-*-
打印u'Пцццц'

似乎打印得很好,没有
u
但是:
打印列表(os.walk(ur'c:\somefoler'))
返回
\u0438\u0442…
为什么第一次打印时不可读?另外,我使用os.walk和变量,我不能将其与
ur
一起使用,我只是想了解如何使我的以下代码与任何文件夹/文件一起工作。我使用os.walk+保存到文件这两种语言似乎都不总是起作用

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 = re.compile(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'
,如果它存在,为什么会有很多次无法转换的异常?我遇到了它,看到了很多关于此类错误的答案。我很难理解,没有办法让它正常工作吗?

试试看

root = ur'c:\somefoler'
for current,dirs,files in os.walk(root):
    for file in files:
        print file, repr(file)

您应该看到正确的内容(在列表中使用的repr旁边)。。。问题是,当你打印一个列表时,它会打印一个
repr
它的项目

这会起作用,但正如你所看到的,我有
os.walk(root)
不能做
urroot
我能找到的替代方法是
.decode('utf-8')
(之前需要再次检查root是否为unicode?)但是,就我在不同答案中看到的情况来看,似乎并不能可靠地处理所有场景,所以我无法理解应该如何正确地完成这项工作啊,是的,我可以在
列表(findit)中更改它('c:\\'
ur
会一直工作吗?文件的这只是dirs文件,我无法像thisos那样轻松更改。walk也有子dirs,文件我还需要将它们与
ur
进行unicode编码。是的,我可以按照您的建议,使用
ur
更改根目录,但子类别和文件仍然是ASCII编码的不是unicode如果我对根使用unicode字符串,我会为文件获取