如何在Jupyter Notebook,Python3上解决FileNotFound错误

时间:2017-10-07 17:35:50

标签: python-3.x

定义时间间隔

def read_yahoo_data(fname):
   pd.read_csv(fname, index_col=0, na_values="null").dropna()

获取日本和美国股票的熊猫数据

toyota     = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/TM.csv')

mitsui     = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/MTU.csv')

mitsubishi = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/MITSY.csv')

apple      = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/APPL.csv')

msft       = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/MSFT.csv')

hpq        = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/HPQ.csv')

nikkei     = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/^N225.csv')

sp500      = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/^GSPC.csv')

这是我使用的代码,文件的路径应该是true。 我正在使用MacOS。

1 个答案:

答案 0 :(得分:0)

pandas csv_read从工作目录开始。所以你有两种方法可以做到这一点:

  1. 传递相对路径(例如“../../../ Users / xxxx / xxx.csv”)
  2. 更改工作目录。
  3. 使用选项2,您的代码将是这样的:

    def read_yahoo_data(fname):
        import pandas as pd
        import os
        os.chdir('/Users/chenyuhan/Downloads/yahoo_finance/')
        return pd.read_csv(fname, index_col=0, na_values="null").dropna()
    

    你需要像这样传递文件名:

    toyota     = read_yahoo_data('TM.csv')
    mitsui     = read_yahoo_data('MTU.csv')
    mitsubishi = read_yahoo_data('MITSY.csv')
    apple      = read_yahoo_data('APPL.csv')
    msft       = read_yahoo_data('MSFT.csv')
    hpq        = read_yahoo_data('HPQ.csv')
    nikkei     = read_yahoo_data('^N225.csv')
    sp500      = read_yahoo_data('^GSPC.csv')