Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
csv error
#1
Any idea what could be causing this error?
def read_to_df(file_path):
    """Read on-disk data and return a dataframe."""
    
    ##read_file = pd.io.parsers.read_csv(file_path)
    df = pd.read_csv(file_path, sep=',')

        
    return read_file

test = read_to_df("C:\Users\Kathleen\Documents\Mike\Emeritus\csvtest.csv")
print(test)
This is the error
Error:
File "<ipython-input-14-7515d68addf8>", line 20 test = read_to_df("C:\Users\Kathleen\Documents\Mike\Emeritus\csvtest.csv") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Reply
#2
The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character., i.e. in combination with some chars (e.g. u) you get escape sequence. Check https://docs.python.org/3/reference/lexi...l#literals

either use forward slash, not backslash or use raw string, e.g.
test = read_to_df(r"C:\Users\Kathleen\Documents\Mike\Emeritus\csvtest.csv")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020