2011-12-09 21 views
2

我得到這個API在Python中運行,並可以得到它來更新.csv文件,但是當我剛剛它打印股票時,我不知道它在哪裏輸出這些數據。這是我的代碼:python println在終端中運行時在哪裏

import urllib 
import os 
import time 

# loop that checks stock prices every 20 seconds and adds them to the file 
while 1: 
    # sometimes this program gives me socket errors so if it does skip this itteration of the loop 
    try: 
     stocks = urllib.urlopen('http://finance.yahoo.com/d/quotes.csv?s=JPM+C&f=snl1c6').read() 
    except IOError: 
     print ("error reading the socket") 
     time.sleep(120) #if we don't sleep here loop constently retrys with no delay 
     continue 
    print stocks 
+0

要將其格式化爲代碼,請在每行代碼前添加4個空格(在您的帖子上按編輯以查看它是如何完成的)。 –

回答

2

它的工作原理。應該沒有問題。

>>> stocks = urllib.urlopen('http://finance.yahoo.com/d/quotes.csv?s=JPM+C&f=snl1c6').read() 
>>> print stocks 
"JPM","JP Morgan Chase &",33.175,"+0.955" 
"C","Citigroup, Inc. N",28.87,"+1.12" 

爲什麼你想在循環中做到這一點?如果成功,您立即進行另一個查詢。

0
python yourprogram.py >> outfile 

在你的評論中,你說你要輸出到文件。這將不斷追加到outfile

0

默認情況下,它打印到sys.stdout。檢查文檔print

相關問題