0

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

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
user1922364
  • 513
  • 2
  • 8
  • 19
  • Try https://stackoverflow.com/a/19089210/2237916 in order to check if is the problem with the encoding of the ipython you are using. – silgon Aug 17 '17 at 13:06
  • 1
    Use a raw string `r'c:\Users\...'` if you have backslashes in your string, or use double backslashes to escape them, or use forward slashes. The `\U` is the start of a Unicode escape sequence otherwise, as the error message says. – Mark Tolonen Aug 17 '17 at 15:36
  • Duplicate of https://stackoverflow.com/a/1347854/235698 – Mark Tolonen Aug 17 '17 at 15:38
  • Possible duplicate of ["Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3](https://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file) – Josh Lee Aug 17 '17 at 16:05

1 Answers1

2

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.

BlackJack
  • 4,476
  • 1
  • 20
  • 25