Python Automatically ignore unicode string

2024/10/15 5:25:46

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).

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 """ 

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.

Or is there a way to tell Python :

file = StopThinkingWithUnicode("MyFilePath")

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 1MyFilePath = "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 : I've tried setting at the first line of the script :

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

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

Answer

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 .

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

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

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

https://en.xdnf.cn/q/117867.html

Related Q&A

How to obtain currency rates from this website converter widget python

How can I implement the currency rates on this website and keep the currencies up to date so that i can access them in python from this website and input and output values and currencies types. I need …

Trying to add sums from a csv file in python

I need to add sums of a csv file. The program is a test for a travel reservation system and the file reads like this:availableSTART,reservations,cancellations,availableEND 20,1,0,18I need to subtract r…

Numerical patterns in Python3 [duplicate]

This question already has answers here:How to print without a newline or space(30 answers)Closed 7 years ago.Im fairly new to programming, i have to start learning it for Uni.I have to make a pattern a…

setsockopt before connect for reactor.connectTCP

I have a small python client which needs a setsockopt after create_socket, but before connect. The non-twisted python code is as follows. How can this be expressed in a twisted environment?create_sock…

Manage quotation marks in XPath (lxml)

I want to extract web elements from the table MANUFACTURING AT A GLANCE in the given website. But the name of the row has (single quote). This is interfering with my syntax. How do I overcome this iss…

exception capture in threads and parent

How do you nicely capture exceptions in Python threads?If you have a threaded python app sys.excepthook does not capture errors in children.When a child raises an exception the parent will continue to…

Writing a program that compares 2 numbers in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Run Python3 without activating the virtual environment

My objective is to run Python 3 code on the AWS Lambda Service, which currently only supports Python 2.7. These are the steps I have done.Since I work on a Mac, setup a docker image similar to the AWS …

Matplotlib functions in tkinter

This is my first python project so I understand that this problem may seem a bit stupid. I am trying to create a Mandelbrot renderer. I am piecing code together from tutorials and code that I understan…

sudo su user -c with arguments not working

I am trying to execute command from python as another "user" with:command = "sudo su user -c player --standard=1 -o 2" subprocess.Popen(command.split(), shell=False, stdin=None, std…