0

I am trying to import a csv file using pandas in python but it is showing me the following error: ##code## Companies = pd.read_csv('C:\Users\salsa\Downloads\1000_Companies.csv')

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

I tried the double slashes, forward slashes and putting 'r' at the front. nothing seems to work. Can anyone help me with this?

thank you

BigBen
  • 46,229
  • 7
  • 24
  • 40

3 Answers3

0

I believe that this answer is what you are looking for. Instead of an 'r', put a 'u' in the front of the string.

Other possible solutions:

Can you open the csv with an editor and show the control characters to provide an example of the csv?

If using notepad++ open the csv file then: view->show symbol-> show all characters do a screenshot around position 2-3 and send it here.

It smells as ascii/unicode problem. if you can and do not lose important features of your dataset, try to convert the csv to ascii, here an example on how to do it otherwise.

  • 1
    The issue is absolutely not the dataset. The issue is the Python source code, hence the SyntaxError, the Python code *doesn't even compile*. – juanpa.arrivillaga Feb 28 '23 at 17:52
0

try checking with the following replacement:


    import csv
    Companies= open(r 'C:\Users\salsa\Downloads\1000_Companies.csv')
    Companies= csv.reader(Companies)

If this is working perfectly then you might have error syntax of the code.

if none are working try this, but cant promise anything. but might worth checking.


    import os
    import pandas as pd
    Companies= "C:\Users\salsa\Downloads\1000_Companies.csv"
    pwd = os.getcwd()
    os.chdir(os.path.dirname(Companies))
    Companies= pd.read_csv(os.path.basename(Companies))
    os.chdir(pwd)

0

Try this, it must be working

import pandas as pd
data = pd.read_csv(r'C:\Users\iffy\Desktop\filename.csv')   
iffy
  • 11
  • 1