Read .csv in pandas from drive F: on Windows 7

I have a CSV file on my F: drive on a 64-bit version of Windows 7 that I would like to read in pandas and manipulate.

None of the examples that I see read anything except a simple file name (for example, "foo.csv").

When I try to do this, I get error messages that do not make the problem clear to me:

import pandas as pd trainFile = "F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv" trainData = pd.read_csv(trainFile) 

The error message says:

 IOError: Initializing from file failed 

Something is missing for me here. Can anyone see this?

Update:

I got additional information:

 import csv if __name__ == '__main__': trainPath = 'F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv' trainData = [] with open(trainPath, 'r') as trainCsv: trainReader = csv.reader(trainCsv, delimiter=',', quotechar='"') for row in trainReader: trainData.append(row) print trainData 

I received a permission error while reading. When I checked the properties of the file, I saw that it was read-only. I was able to read 892 lines successfully after unchecking.

Now pandas works. No need to move the file or change the path. Thanks for watching.

+6
source share
4 answers

I canโ€™t promise that this will work, but itโ€™s worth it:

 import pandas as pd import os trainFile = "F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv" pwd = os.getcwd() os.chdir(os.path.dirname(trainFile)) trainData = pd.read_csv(os.path.basename(trainFile)) os.chdir(pwd) 
+8
source

If you are sure that the path is correct, make sure that other programs did not open the file. I got this error once, and closing the Excel file made the error go away.

+2
source

I also had a problem, and I decided that it was allowed.

Check the path to the file

I originally had a path like

 dfTrain = pd.read_csv("D:\\Kaggle\\labeledTrainData.tsv",header=0,delimiter="\t",quoting=3) 

This returned an error because the path was wrong. Then I changed the path as shown below. This works fine.

 dfTrain = dfTrain = pd.read_csv("D:\\Kaggle\\labeledTrainData.tsv\\labeledTrainData.tsv",header=0,delimiter="\t",quoting=3) 

This is due to the fact that my previous path was not correct. Hope he is upside down

+2
source

A better solution is to use literal strings such as r'pathname \ filename 'rather than' pathname \ filename '. See Lexical Analysis for more details.

0
source

Source: https://habr.com/ru/post/946637/


All Articles