Python自动忽略unicode字符串 [英] Python Automatically ignore unicode string

查看:33
本文介绍了Python自动忽略unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找自动导入一些文件,但由于我在 Windows 上,我收到了 unicode 错误(因为C:\Users\...").我一直在寻找更正这个错误并找到了一些提示(使用 r"MyString" 或 u"MyString" 表示原始和 unicode 字符串)并且我已被定向到此页面 (https://docs.python.org/3/howto/unicode.html).

I've been searching to automatically import some files but since I'm on Windows i got the unicode error (because of the "C:\Users\..."). I've been looking to correct this error and found some hints (using r"MyString" or u"MyString" for raw and unicode strings) and I have been directed to this page (https://docs.python.org/3/howto/unicode.html).

但由于我的问题是关于自动导入一些文件的 GUI 界面,我还没有想出办法.

But since my problem is about a GUI interface to automatically import some files, I haven't figured out the way to do it.

我会在这里给你我的提示:

I'll leave you my hints right here :

 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 理解我想复制 ru 后面的路径.

One of those two seems to be nice but I don't know how to let python understands that I want to copy the path behind the r or the u.

或者有没有办法告诉 Python :

Or is there a way to tell Python :

file = StopThinkingWithUnicode("MyFilePath")

我也看到了这个链接(在 python mkdtemp 中处理 unicode 用户名) 但也不起作用(由于 Python2.7 编写,我已经更正了 print() 函数,而我使用的是 3.5)

I've also see this link (Deal with unicode usernames in python mkdtemp) but doesn't work neither (I've corrected the print() function because of the Python2.7 write and I'm on 3.5)

我忘了发布回溯,所以它在那里:

I've forgotten to post the traceback so there it is :

  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

有人可以帮我提供一些提示或链接吗?感谢您的帮助.

Could someone help me with me some hints or link? Thank for your help.

PS:我试过在脚本的第一行设置:

PS : I've tried setting at the first line of the script :

 # -*- coding: latin-1 -*- 

(我有 *.xl、*.csv、*.sas7bdat、*.txt 文件)

(I have *.xl , *.csv, *.sas7bdat, *.txt files)

推荐答案

这是 Windows 路径的一个非常常见的问题.我怀疑人们偶然发现了它,并通过将烦人的"小写字母与转义序列 (\n,\t,\b,\a,\v,\x ...) 大写.它有效,除了 \U (这是 unicode 转义序列)和 \N .

That's a very frequent issue with windows paths. I suspect that people stumble upon it, and figure out a way by putting the "annoying" lowercase letters matching escape sequences (\n,\t,\b,\a,\v,\x ...) in upper case. It works, except for \U (which is unicode escape sequence) and \N .

真正的解决方案是使用 raw 前缀来逐字处理反斜杠:

The real solution is to use raw prefix to treat backslashes literally:

MyFilePath = r"C:\Users\MyUser\Desktop\Projet\05_Statistiques\Data\MyFileName.xlsx"
             ^

我关于大写避免错误的理论证实了.检查这个问题中的路径:csv python 可以处理的最大行数?

my theory about "bug avoidance by uppercase confirms. Check the path in this question: Largest number of rows in a csv python can handle?

这篇关于Python自动忽略unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆