How to open my files in data_folder with pandas using relative path?

前端 未结 11 2439
庸人自扰
庸人自扰 2020-12-22 23:40

I\'m working with pandas and need to read some csv files, the structure is something like this:

folder/folder2/scripts_folder/script.py

folder/fol

11条回答
  •  一生所求
    2020-12-23 00:32

    I was also looking for the relative path version, this works OK. Note when run (Spyder 3.6) you will see (unicode error) 'unicodeescape' codec can't decode bytes at the closing triple quote. Remove the offending comment lines 14 and 15 and adjust the file names and location for your environment and check for indentation.

    -- coding: utf-8 --

    """ Created on Fri Jan 24 12:12:40 2020

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

    Demonstrates: Load a csv not in the CWD by specifying relative path - windows version

    @author: Doug

    From CWD C:\Users\Doug\.spyder-py3\Data Camp\pandas we will load file

    C:/Users/Doug/.spyder-py3/Data Camp/Cleaning/g1803.csv
    

    """

    import csv
    
    trainData2 = []
    
    with open(r'../Cleaning/g1803.csv', 'r') as train2Csv:
    
      trainReader2 = csv.reader(train2Csv, delimiter=',', quotechar='"')
    
      for row in trainReader2:
    
          trainData2.append(row)
    
    print(trainData2)
    

提交回复
热议问题