user1922364
user1922364

Reputation: 573

unicodeescape error when importing a CSV File in Python 3

It's a very basic question but I've tried many things. My last code is:

import csv
with open ('C:\Users\Michel Spiero\Desktop\Base de dados para curso de Python/enrollments.csv') as csvfile:
    readCSV =csv.reader(csvfile, delimiter=',')

    for row in readCSV:
        print(row)

I am getting this error:

File "<ipython-input-9-3103e7dc9e55>", line 3
    with open ('C:\Users\Michel Spiero\Desktop\Base de dados para curso de Python/enrollments.csv') as csvfile:
              ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

What Should I do?

Thanks

Upvotes: 0

Views: 2308

Answers (1)

BlackJack
BlackJack

Reputation: 4679

Backslash U (\U) has a special meaning in string literals. String and Bytes literals in the documentation says for \Uxxxxxxxx the meaning is „Character with 32-bit hex value xxxxxxxx“.

So you have to escape at least the backslash before the U of Users, or put an r in front of the string so no backslash has a special meaning.

Upvotes: 2

Related Questions