Home > database >  v SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2
v SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2

Time:12-13

I have tried putting 'r' before my code, as well as changed the code with back slash/ front slash but don't seem to be able to upload my csv file into python. is there a particular reason for this error?

please help me fix this!!

CodePudding user response:

Make sure to set also the encoding parameter to 'utf-8'. If the path to file is correct, bellow code should fix the issue:

pd.read_csv('C:/Users/payalsachdev/Desktop/cars.csv', encoding='utf-8')

If this still doesn't work you can go further and ignore malformed data and continue by setting encoding_errors to 'ignore':

pd.read_csv('C:/Users/payalsachdev/Desktop/cars.csv', encoding='utf-8', encoding_errors='ignore')

You can check more details here: pandas.read_csv, and also why 'utf-8' encoding is not always the default one: python 3 default encoding

CodePudding user response:

I was able to import the data set thanks so much for your help

I am trying to create a new column, I want col 3 to give me results if col 1 == 1 and col2 == 0 I want col 3 to give me 1..and I want the other values to remain unchanged..which code can I use for this?

ex: Col 1. col 2 Col 3 1 0 0 1 0 1

  • Related