Python Forum
Importing a csv file with spyder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing a csv file with spyder
#1
Hi Everyone I am trying to import a csv file called 'train' in Spyder and it is not working.

code:
train = pd.read_csv('C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis\train.csv')
train[:4]
I get this error below:

File "<ipython-input-13-93db4cee1b88>", line 1
train = pd.read_csv('C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis\train.csv')
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

This is really holding me back any help is greatly appreciated. My text book does this code:
train = pd.read_csv('datasets/titanic/train.csv')
I don't really get how the path does not have a drive but anyhow can anyone please help?

The file is saved in this location on my computer: C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis\train.csv'.

Thanks a lot
Reply
#2
The path from the book doesn't have a drive because is local to the script's location. datasets folder is in the same folder as the script is.

In Python, the backslash is used for the escape characters. For example the new line - '\n'. You can use / for the path as it is in web addresses even on Windows system. Try it.
Also, you can try to use a raw string for the path: r'C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis\train.csv'.

Check the path for mistypes.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
The r trick worked, thank you so much.

So python for paths does not like \? I just right clicked and copy pasted the location which had \.
Reply
#4
I am not saying that. Python will work with both \ and /. But it's safe to use / for paths because the \ is used in Python for escape characters. That is why r'' forks. The string is interpreted as it is. Another way is to use \\ for any \ in the path.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 721 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  Problem with importing Python file in Visual Studio Code DXav 7 5,070 Jun-15-2022, 12:54 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,365 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Importing a function from another file runs the old lines also dedesssse 6 2,542 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,606 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  importing a CSV file into Python russoj5 1 2,946 Aug-02-2020, 12:03 AM
Last Post: scidam
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,097 Jun-29-2020, 08:51 PM
Last Post: buran
  importing CSV file into a OOP Class table using Python faruk61 1 2,951 Apr-15-2020, 12:00 PM
Last Post: faruk61
  importing CSV file into a HTML table using Python trybakov 1 2,278 Feb-22-2020, 09:47 PM
Last Post: scidam
  Importing variables from another file IILawrenceII 7 9,996 Jan-18-2020, 12:31 PM
Last Post: IILawrenceII

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020