neo33
neo33

Reputation: 1879

How to create a data frame with the following csv?

Hello I am working with a csv that looks as follows:

field1,field2,field3
user1,"information",1
user2,"information",0
user3,information,2

I would like to créate a data frame from this using pandas I tried:

import pandas as pd
df1=pd.read_csv("C:\Users\acamagon\Downloads\MyComments.csv",sep=',',columns=['field1','field2','field3'])

print(df1)

However I got the following error, I would like to appreciate any suggestion to overcome this issue:

  File "<ipython-input-53-ba9e69f7c66b>", line 3
    df1=pd.read_csv("C:\Users\acamagon\Downloads\MyComments.csv",sep=',',columns=['field1','field2','field3'])
                   ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Upvotes: 1

Views: 96

Answers (1)

Victor Chubukov
Victor Chubukov

Reputation: 1375

The problem is that python is treating the backslashes in your filename string as escape characters.

You can either use forward slashes instead of backslashes to specify the path

"C:/Users/acamagon/Downloads/MyComments.csv"

or preface the string with r to specify that python should not treat the backslashes as special characters

r"C:\Users\acamagon\Downloads\MyComments.csv"

As a side note, you don't need to specify the column names in the read_csv call unless you want to change them -- pandas will try to read them from the first row.

Upvotes: 1

Related Questions