Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing data
#1
Hi everyone,

I cannot seem to import data (I'm using spyder). I do the following code:

data = pd.read_csv('Python for Data Analysis/chap 6.csv'
data.head()


for that code I get the error:
FileNotFoundError: File b'Python for Data Analysis/chap 6.csv' does not exist

I also try the full path:
data = pd.read_csv('C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis/chap 6.csv'
data.head()


I get the error:
data = pd.read_csv('C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis\chap 6.csv')
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Any help is appreciated.
Thanks
Reply
#2
You could try this
from pathlib import Path

filename = Path("source_data/csv/raw_data.csv")

data = pd.read_csv('filename', encoding='utf-8')

The original csv file might also be contributing to the issues as well - download Notepad++
Create a new file in Notepad++ -> Go to Encoding -> Encode in UTF-8 -> Copy-paste the contents -> save the file as .csv
Reply
#3
I get same issue when I substitute this link in
filename = Path("C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis/csv/chap 6.csv")
Reply
#4
It may have to do with One Drive - take the file off one drive and store it locally - avoid spaces in your directory naming - If you need a space use - or _ . Or you can try this - let say your directory path is c:\new dir - you could try cd new\dir or cd " new dir ".

I found this code - by googling - copy the code your to editor then save it as win2ubu.py - the rest is explained in the code itself.
This is not my code all credit goes to Joe Dorocak aka Joe Codeswell (JoeCodeswell.com). It may help you to convert your windows directory paths to linux paths.
#!/usr/bin/python
#! python3
#! python2
# -*- coding: utf-8 -*-
"""win2ubu.py changes WINFILEPATH Printing UBUNTU_FILEPATH
Author: Joe Dorocak aka Joe Codeswell (JoeCodeswell.com)
Usage:   win2ubu.py WINFILEPATH
Example: win2ubu.py "C:\\1d\ProgressiveWebAppPjs\\Polymer2.0Pjs\\PolymerRedux\\zetc\\polymer-redux-polymer-2"
    prints /mnt/c/1d/ProgressiveWebAppPjs/Polymer2.0Pjs/PolymerRedux/zetc/polymer-redux-polymer-2
        N.B. spaceless path needs quotes in BASH on Windows   but NOT in Windows DOS prompt!
"""
import sys,os

def winPath2ubuPath(winpath):
    # d,p = os.path.splitdrive(winpath) # NG only works on windows!
    d,p = winpath.split(':')
    ubupath = '/mnt/'+d.lower()+p.replace('\\','/')   
    print (ubupath)
    return ubupath

NUM_ARGS = 1
def main():
    args = sys.argv[1:]
    if len(args) != NUM_ARGS or "-h" in args or "--help" in args:
        print (__doc__)
        sys.exit(2)
    winPath2ubuPath(args[0])

if __name__ == '__main__':
    main()
Reply
#5
(May-11-2018, 03:37 AM)Scott Wrote: I get same issue when I substitute this link in
filename = Path("C:\Users\SGrah\OneDrive\Documents\Python Scripts\Python for Data Analysis/csv/chap 6.csv")
Your path will not work because of escape character \U.
Never use use single backslash (\) that way in path,because of escape character.
Look at this post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ssues with importing data from ODBC Slavek_d 1 1,418 Feb-01-2022, 09:57 AM
Last Post: ibreeden
  Importing python data to Textfile or CSV yanDvator 0 1,763 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,154 Jun-29-2020, 08:51 PM
Last Post: buran
  Importing csv data into sql lite cant figure it out. jimmyvegas29 5 3,772 Jul-16-2018, 02:02 AM
Last Post: jimmyvegas29

Forum Jump:

User Panel Messages

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