Read a .csv into pandas from F: drive on Windows 7

后端 未结 6 1102
广开言路
广开言路 2020-12-19 02:04

I have a .csv file on my F: drive on Windows 7 64-bit that I\'d like to read into pandas and manipulate.

None of the examples I see read from anything other than a s

相关标签:
6条回答
  • 2020-12-19 02:41

    I also got the same issue and got that resolved .

    Check your path for the file correctly

    I initially had the 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 have changed the path as below.This is working fine.

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

    This is because my earlier path was not correct.Hope you get it reolved

    0 讨论(0)
  • 2020-12-19 02:48

    I cannot promise that this will work, but it's worth a shot:

    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)
    
    0 讨论(0)
  • 2020-12-19 02:49

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

    0 讨论(0)
  • 2020-12-19 02:52

    If you're sure the path is correct, make sure no other programs have the file open. I got that error once, and closing the Excel file made the error go away.

    0 讨论(0)
  • 2020-12-19 02:57

    Try this:

    import os
    import pandas as pd
    
    
    trainFile = os.path.join('F:',os.sep,'Projects','Python','coursera','intro-to-data-science','train.csv' )
    trainData = pd.read_csv(trainFile)
    
    0 讨论(0)
  • 2020-12-19 03:02

    This happens to me quite often. Usually I open the csv file in Excel, and save it as an xlsx file, and it works.

    so instead of

    df = pd.read_csv(r"...\file.csv")
    

    Use:

    df = pd.read_excel(r"...\file.xlsx")
    
    0 讨论(0)
提交回复
热议问题