2015-10-04 26 views
0
import pandas as pd 
import os 
import time 
from datetime import datetime 

path = "C:\WinPython-32bit-2.7.9.5\python- 2.7.9\Lib\idlelib\MuditPracticals\intraQuarter\intraQuarter" 

def Key_Stats(gather="Total Debt/Equity (mrq)"): 
    statspath = path+'/_KeyStats' 
    stock_list = [x[0] for x in os.walk(statspath)] 
df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio','Price','SP500']) 

sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv") 

for each_dir in stock_list[1:25]: 
    each_file = os.listdir(each_dir) 
    ticker = each_dir.split("\\")[3] 
    if len(each_file) > 0: 
     for file in each_file: 
      date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html') 
      unix_time = time.mktime(date_stamp.timetuple()) 
      full_file_path = each_dir+'/'+file 
      source = open(full_file_path,'r').read() 
      try: 
       value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]) 

       try: 
        sp500_date = datetime.fromtimestamp(unix_time).strftime('%Y-%m-%d') 
        row = sp500_df[(sp500_df.index == sp500_date)] 
        sp500_value = float(row["Adjusted Close"]) 
       except: 
        sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d') 
        row = sp500_df[(sp500_df.index == sp500_date)] 
        sp500_value = float(row["Adjusted Close"]) 


       stock_price = float(source.split('</small><big><b>')[1].split('</b></big>')[0]) 
       #print("stock_price:",stock_price,"ticker:", ticker) 



       df = df.append({'Date':date_stamp, 
           'Unix':unix_time, 
           'Ticker':ticker, 
           'DE Ratio':value, 
           'Price':stock_price, 
           'SP500':sp500_value}, ignore_index = True) 
      except Exception as e: 
       print "hello" 

save = gather.replace(' ','').replace(')','').replace('(','').replace('/','')+('.csv') 
print(save) 
df.to_csv(save) 


Key_Stats() 

編譯時錯誤在Spyder的IO錯誤:CSV文件不存在,儘管它存在於指定給定位置

File "<ipython-input-1-dfafbc7450e8>", line 1, in <module> 
     runfile('C:/WinPython-32bit-2.7.9.5/python- 2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py', wdir='C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals') 

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile 
    execfile(filename, namespace) 

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 


File "C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py", line 56, in <module> 
    Key_Stats() 

File "C:/WinPython-32bit-2.7.9.5/python-2.7.9/Lib/idlelib/MuditPracticals/data_organisation1.py", line 13, in Key_Stats 
    sp500_df = pd.DataFrame.from_csv("YAHOO-INDEX_GSPC.csv") 

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\core\frame.py", line 1036, in from_csv 
    infer_datetime_format=infer_datetime_format) 


File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 474, in parser_f 
    return _read(filepath_or_buffer, kwds) 


File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 250, in _read 
    parser = TextFileReader(filepath_or_buffer, **kwds) 

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 566, in __init__ 
    self._make_engine(self.engine) 

File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 705, in _make_engine 
    ``self._engine = CParserWrapper(self.f, **self.options) 


File "C:\WinPython-32bit-2.7.9.5\python-2.7.9\lib\site-packages\pandas\io\parsers.py", line 1072, in __init__ 
    self._reader = _parser.TextReader(src, **kwds) 

File "pandas\parser.pyx", line 350, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:3160) 

File "pandas\parser.pyx", line 594, in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5905) 

IOError: File YAHOO-INDEX_GSPC.csv does not exist 

儘管文件存在於該位置

IO ERROR occurs at compile time 這是給IO錯誤以及爲什麼它在其他IDLE熊貓模塊沒有找到,但在Spyder中沒有pandas Error

+0

你的路徑看起來無效,你需要逃避反斜槓或用'r'所以'路徑= R「C前綴使用原始字符串:\ WIN ......'應該可以工作 – EdChum

+0

根本不工作,現在也顯示IO錯誤csv文件未找到。 –

回答