Python Forum
Importing data - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Importing data (/thread-10037.html)



Importing data - Scott - May-10-2018

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


RE: Importing data - petertyler - May-10-2018

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


RE: Importing data - Scott - May-11-2018

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")



RE: Importing data - petertyler - May-12-2018

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()



RE: Importing data - snippsat - May-12-2018

(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.