Python 文件夹路径中的编码问题
我有一个文件夹路径,其中有一些西里尔字母,可以停止另一个脚本的进程。我需要得到一个没有西里尔字母的文件夹路径,但是python不会返回它 文件夹是Python 文件夹路径中的编码问题,python,encoding,utf,cyrillic,Python,Encoding,Utf,Cyrillic,我有一个文件夹路径,其中有一些西里尔字母,可以停止另一个脚本的进程。我需要得到一个没有西里尔字母的文件夹路径,但是python不会返回它 文件夹是C:\Users\PereverzevPV\Documents\crmk_цц 我有: import os folder = os.getcwd() print folder 无论我设置了什么编码,输出都是空的。我需要去 'C:\\Users\\PereverzevPV\\Documents\\\xd0\x98\xd0\xbd\xd0\xb2' 这
C:\Users\PereverzevPV\Documents\crmk_цц
我有:
import os
folder = os.getcwd()
print folder
无论我设置了什么编码,输出都是空的。我需要去
'C:\\Users\\PereverzevPV\\Documents\\\xd0\x98\xd0\xbd\xd0\xb2'
这可能会有帮助。您的问题是,您没有转义
\
:\U
、\P
、\D
或\imk
是无效的utf-8字符
# coding: UTF-8
txt = r"C:\Users\PereverzevPV\Documents\Инв"
txt = "C:\\Users\\PereverzevPV\\Documents\\Инв"
esc_txt = txt.encode('utf-8')
print(esc_txt)
这可能会有帮助。您的问题是,您没有转义
\
:\U
、\P
、\D
或\imk
是无效的utf-8字符
# coding: UTF-8
txt = r"C:\Users\PereverzevPV\Documents\Инв"
txt = "C:\\Users\\PereverzevPV\\Documents\\Инв"
esc_txt = txt.encode('utf-8')
print(esc_txt)
如果以unicode格式传递文件路径,则返回值也将以unicode格式传递:
>>> for dirpath, dirnames, filenames in os.walk(u"D:\\SO"):
print dirnames
[u'\u0142', u'\u0418\u043d\u0432']
也许您可以使用它,然后根据需要重新组合路径。如果您以unicode格式传递文件路径,则返回值也将以unicode格式:
>>> for dirpath, dirnames, filenames in os.walk(u"D:\\SO"):
print dirnames
[u'\u0142', u'\u0418\u043d\u0432']
也许您可以使用它,然后根据需要重新组合路径。尝试以下方法:
# coding: UTF-8
import os
os.getcwd().encode('utf-8').decode('utf-8')
我们需要对CWD进行编码,然后对其进行解码
# coding: UTF-8
import os
os.getcwd().encode('utf-8').decode('utf-8')
我们需要对CWD进行编码,然后对其进行解码添加到您的第一个脚本:#编码:UTF-8Added。仍然可以在脚本的第一个部分添加以下内容:#编码:UTF-8Added。仍然得到这个
C:\Users\PereverzevPV\Documents\峈峈峈峈
它给出了一个错误UnicodeDecodeError:'ascii'编解码器无法解码第32位的字节0xd0:ordinal not in range(128)
它给出了一个错误UnicodeDecodeError:'ascii'编解码器无法解码第32位的字节0xd0:ordinal not in range(128)