• Register
0 votes
422 views

Problem :

I want to read my .csv file into Python (Spyder) but I am facing the error.

Please find below my code :

import csv
mydata = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener")
mydata = csv.reader(mydata)  
print(mydata)

I face the following error:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

The String literals can be enclosed within either the single quotes (i.e. '...') or the double quotes (i.e. "..."). They can also be enclosed in the matching groups of either three single or double quotes (these are usually referred to as triple-quoted strings).

The backslash character (i.e. \) can be used to escape the characters which otherwise will have a special meaning, such as the newline, backslash itself, or the quote character. String literals may be optionally prefixed with a letter either r or R. Such strings are called as raw strings and used in different rules for the backslash escape sequences.

In the triple-quoted stringsunescaped newlines and quotes both are allowed , except that the three unescaped quotes in the row terminate the string.

Unless an r or R prefix is present, the escape sequences in strings are interpreted according to rules similar to be used by Standard C.

9 7 4
38,600 points
0 votes

Solution:

Since it is an escape failure, the simplest solution is not to use “escape”.

Solution 1: Add a “r” character in the beginning of string.

title = r'\uFFEF'

In Python, when we add a “r” character, it’s meaning a complete string.

Solution 2: Change “\” to be “/”.

open("C:\Users\Clay\Desktop\test.txt")

Change to:

open("C:/Users/Clay/Desktop/test.txt")

Solution 3: Change “\” to be “\\”.

open("C:\Users\Clay\Desktop\test.txt")

To be:

open("C:\\Users\\Clay\\Desktop\\test.txt")

This error occurs because you are using a normal string as a path. You can use one of the following solutions to fix your problem.

1. Just put r before your normal string it converts normal string to raw string:
pandas.read_csv(r"C:\Users\DeePak\Desktop\myac.csv")

2.

pandas.read_csv("C:/Users/DeePak/Desktop/myac.csv")

3.

pandas.read_csv("C:\\Users\\DeePak\\Desktop\\myac.csv")

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 495 views
495 views
Problem : Please find below my code for your reference. import os for root, dirs, files in os.walk('Path'):      for file in files:          if file.endswith('.c'):              with open(os.path.join(root, file)) as f:                     for line in f: ... already tried setting it with the open(os.path.join(root, file),'r',encoding='cp932') as f: but got the same above error
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
1 answer 1.8K views
1.8K views
Problem : I am new to the Python, I am using Python-2.6 CGI scripts but facing following error in the server log while I was doing json.dumps(), Traceback (most recent call last): File "/etc/mongodb/server/cgi-bin/getstats.py", line 135, in <module> print json.dumps ... = datetime.datetime.strftime(now, '%Y-%m-%dT%H:%M:%S.%fZ') print json.dumps({'current_time': now}) // I guess this is the culprit
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 828 views
828 views
Problem : Getting bellow error while executing numpy arrays unicodedecodeerror: 'ascii' codec can't decode byte 0x90 in position 614: ordinal not in range(128)
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
2 answers 6.5K views
6.5K views
Problem : I have encountered the following error while compiling "process.py" python tools/process.py --input_dir data -- operation resize --outp ut_dir data2/resize data/0.jpg -> data2/resize/0.png Traceback (most recent call last): File "tools/process.py", line 235, in <module ... 0xff in position 0: invalid start byte What may be the cause of the error? I am using Python's version as 3.5.2.
asked Nov 22, 2019 peterlaw 6.9k points
0 votes
1 answer 2K views
2K views
Problem : MySample.csv contains the below details : NAME Id No Dept Tommy 1 12 CS Jimmy 2 35 EC Bonny 3 21 IT Franky 4 61 EE And my Python file contains the below code : import csv myifile = open('mysample.csv', "rb") read = csv.reader(myifile) for row in read : ... in for row in read : _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?) How should I fix it?
asked Jan 2, 2020 alecxe 7.5k points
2 votes
2 answers 272 views
272 views
Problem: I have a simple python program I wrote recently. But unfortunately, I can&rsquo;t compile and run my program. It shows me an error &ldquo;syntaxerror: keyword can't be an expression&rdquo; Where I am being wrong in my program? Any idea? The snippet is attached below: name = input("Please ... a Gentleman!") else: print(name + " I don't know him and " + name = " Who is he? ") Thanks, guys.
asked Mar 11, 2020 Gavin 15.3k points
0 votes
1 answer 1.1K views
1.1K views
Problem : When I run my Python code I get the following exception: _csv.error: iterator should return strings, not bytes (did you open the file in text mode?)
asked Nov 18, 2019 peterlaw 6.9k points
0 votes
1 answer 4 views
4 views
Problem: I am trying to define a function to make the perimeter of a rectangle. Here is the code: width = input() height = input() def rectanglePerimeter(width, height): return ((width + height)*2) print(rectanglePerimeter(width, height)) I think I haven't left any arguments opened or anything like that.
asked 6 days ago Mashhoodch 5.6k points
0 votes
2 answers 71 views
71 views
I am a beginner to python and am at the moment having trouble using the command line. I have a script test.py (which only contains print("Hello.")), and it is located in the map C:\Python27. In my system variables, I have specified python to be C:\Python27 (I have ... but when I do so I get this: File "<stdin>", line 1 python test.py ^ SyntaxError: invalid syntax What is wrong? Thanks in advance!
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 30 views
30 views
Hello house, I would like to know how to solve this Python SyntaxError: Non-ASCII character '\xe2' in file. Thanks
asked Oct 23, 2020 psandprop 2.4k points