python os.walk和unicode错误

python os.walk和unicode错误,python,unicode,encoding,utf-8,visual-studio-code,Python,Unicode,Encoding,Utf 8,Visual Studio Code,两个问题: 1.为什么 In [21]: ....: for root, dir, file in os.walk(spath): ....: print(root) 打印整棵树,但是 In [6]: for dirs in os.walk(spath):

两个问题: 1.为什么

In [21]:                                                                                   
   ....:     for root, dir, file in os.walk(spath):
   ....:         print(root)
打印整棵树,但是

In [6]: for dirs in os.walk(spath):                             
...:     print(dirs)    
被这个unicode错误阻塞了吗

UnicodeEncodeError: 'charmap' codec can't encode character '\u2122' in position 1477: character maps to <undefined>
一切都没有效果

这是在Windows10上使用python 3.4.3和VisualStudio代码1.6.1完成的。Visual Studio代码中的默认设置包括:

//读写时使用的默认字符集编码 文件夹。 “files.encoding”:“utf8”

python 3.4.3 visual studio代码1.6.1 ipython 3.0.0

更新编辑 我在崇高的文本REPL中再次尝试了这一点,运行了一个脚本。以下是我得到的:

# -*- coding: utf-8 -*-
import os

spath = 'C:/Users/Semantic/Documents/Align' 

with open('os_walk4_align.txt', 'w') as f:
    for path, dirs, filenames in os.walk(spath):
        print(path, dirs, filenames, file=f)

Traceback (most recent call last):
File "listdir_test1.py", line 8, in <module>
print(path, dirs, filenames, file=f)
File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2605' in position 300: character maps to <undefined>
#-*-编码:utf-8-*-
导入操作系统
spath='C:/Users/Semantic/Documents/Align'
将open('os_walk4_align.txt','w')作为f:
对于os.walk(spath)中的路径、目录和文件名:
打印(路径、目录、文件名、文件=f)
回溯(最近一次呼叫最后一次):
文件“listdir_test1.py”,第8行,在
打印(路径、目录、文件名、文件=f)
文件“C:\Python34\lib\encodings\cp1252.py”,第19行,在encode中
返回codecs.charmap\u encode(输入、自身错误、编码表)[0]
UnicodeEncodeError:“charmap”编解码器无法对位置300中的字符“\u2605”进行编码:字符映射到

此代码只有217个字符长,那么“position 300”从何而来?

您输出到的控制台默认不支持非ASCII。您需要使用
str.encode('utf-8')

这对字符串有效,而不是列表。所以
print(dirs).encode('utf=8')
不起作用,它是
utf-8
,而不是
utf=8

使用列表理解打印列表,如:

>>> print([s.encode('utf-8') for s in ['a', 'b']])
['a', 'b']
>>> print([d.encode('utf-8') for d in dirs])  # to print `dirs`
下面是一个测试用例:

C:\TEST
├───dir1
│       file1™
│
└───dir2
        file2
下面是一个脚本(Python 3.x):

以下是在支持UTF-8(本例中为Python)的IDE上的输出:

这是我的Windows控制台上的输出,默认为
cp437

c:\test
c:\test\dir1
c:\test\dir2
('c:\\test', ['dir1', 'dir2'], [])
Traceback (most recent call last):
  File "C:\test.py", line 9, in <module>
    print(dirs)
  File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2122' in position 47: character maps to <undefined>
以UTF-8编码的os_walk4_align.txt的内容:

c:\test ['dir1', 'dir2'] []
c:\test\dir1 [] ['file1™']
c:\test\dir2 [] ['file2']

我想你的意思是“unicode”,而不是“unicorn”。我正在Windows10上测试新的VisualStudio代码,这就是我使用它的原因,正如我所说的,默认值已经设置为utf-8。此外,我在Sublime文本中尝试了这一点,但仍然会出现unicode错误,尽管是不同的错误。设置源编码(
#coding:utf8
)与输出编码无关。从错误
cp1252
可以看出,它是输出编码,不支持打印到终端的字符。解决此问题的最简单方法是使用UTF-8编码写入文件,而不是打印到显示器,或者使用支持UTF-8输出的Python IDE。我不熟悉Sublime文本,但它可能也有调整输出编码的方法。叮!叮!叮!我们赢了!撇开我自己的“=”打字错误不谈,我在打印行上有编码,而它本应该在参数中。你详细的回答帮了大忙。谢谢
c:\test
c:\test\dir1
c:\test\dir2
('c:\\test', ['dir1', 'dir2'], [])
('c:\\test\\dir1', [], ['file1™'])
('c:\\test\\dir2', [], ['file2'])
c:\test
c:\test\dir1
c:\test\dir2
('c:\\test', ['dir1', 'dir2'], [])
Traceback (most recent call last):
  File "C:\test.py", line 9, in <module>
    print(dirs)
  File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2122' in position 47: character maps to <undefined>
import os

spath = r'c:\test'

with open('os_walk4_align.txt', 'w', encoding='utf8') as f:
    for path, dirs, filenames in os.walk(spath):
        print(path, dirs, filenames, file=f)
c:\test ['dir1', 'dir2'] []
c:\test\dir1 [] ['file1™']
c:\test\dir2 [] ['file2']