【发布时间】:2017-06-12 01:48:30
【问题描述】:
我一直在寻找自动导入一些文件的方法,但由于我使用的是 Windows,所以我遇到了 unicode 错误(因为“C:\Users\...”)。 我一直在寻找更正此错误并找到一些提示(使用 r"MyString" 或 u"MyString" 用于原始和 unicode 字符串),我已被定向到此页面(https://docs.python.org/3/howto/unicode.html)。
但是由于我的问题是关于自动导入某些文件的 GUI 界面,所以我还没有想出办法。
我会在这里给你我的提示:
file = file.replace('\\', '//')
file = r"MyFilePath"
file = u"MyFilePath"
file = os.path.abspath("MyFilePath")
file = "MyFilePath".decode('latin1')
""" isn't correct because a string has no attribute 'decode' of course """
这两个中的一个似乎不错,但我不知道如何让 python 明白我想复制 r 或 u 后面的路径。
或者有没有办法告诉Python:
file = StopThinkingWithUnicode("MyFilePath")
我也看到了这个链接(Deal with unicode usernames in python mkdtemp),但也不起作用(我已经更正了 print() 函数,因为 Python2.7 写入并且我在 3.5 上)
我忘记发布回溯所以它就在那里:
MyFilePath = "C:\Users\MyUser\Desktop\Projet\05_Statistiques\Data\MyFileName.xlsx"
File "<ipython-input-13-d8c2e72a6d3f>", line 1
MyFilePath = "C:\Users\MyUser\Desktop\Projet\05_Statistiques\Data\MyFileName.xlsx"
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
有人可以帮我一些提示或链接吗? 感谢您的帮助。
PS:我尝试在脚本的第一行设置:
# -*- coding: latin-1 -*-
(我有 *.xl、*.csv、*.sas7bdat、*.txt 文件)
【问题讨论】:
-
只需使用 / 或 \\ 而不是 \
-
用户不应该这样做,这是@JayParikh 的主要问题之一
标签: python windows user-interface unicode python-3.5