我在互联网的最深处搜索,却似乎找不到解决我的问题的方法。我正在使用PyCharm Community Edition 2017.1.1,在MacOS 10.12.4上使用Python 3.6.1和curses库编写以下代码。
#!/usr/local/bin/python3
import sys
import urllib.request
import json
import time
from datetime import datetime
from datetime import timezone
import curses
import ssl
class Stock:
def stockFromYahooWebService(ticker):
url = "https://finance.yahoo.com/webservice/v1/symbols/{}/quote?format=json&view=detail".format(ticker.upper())
user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1"
urlrequest = urllib.request.Request(url, data=None, headers={"User-Agent": user_agent})
ssl._create_default_https_context = ssl._create_unverified_context
urlcontent = json.loads(urllib.request.urlopen(urlrequest).read().decode())
fields = urlcontent['list']['resources'][0]['resource']['fields']
return fields
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1)
try:
if len(sys.argv) == 2:
ticker = sys.argv[1]
while ('true') :
stdscr.erase()
fields = Stock.stockFromYahooWebService(ticker)
utctime = datetime.strptime(fields['utctime'], "%Y-%m-%dT%H:%M:%S+%f")
localtime = utctime.replace(tzinfo=timezone.utc).astimezone(tz=None)
localtimeString = localtime.strftime("%I:%M%p %Z %m/%d/%y")
stdscr.addstr(0, 0, "{} ({})".format(fields['issuer_name'], fields['symbol']))
stdscr.addstr(1, 0, "Price: ${:,.2f}".format(float(fields['price'])))
stdscr.addstr(2, 0, "Change: {:+,.2f} ({:+,.2f}%)".format(float(fields['change']),float( fields['chg_percent'])))
stdscr.addstr(3, 0, "Volume: {:,}".format(int(fields['volume'])))
stdscr.addstr(4, 0, "Last Trade: {}".format(localtimeString))
stdscr.refresh()
time.sleep(5)
finally:
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()
if len(sys.argv) != 2 :
print('Usage: ./rtquote.py <ticker>')
我知道PyCharm使用自己的终端来显示输出,但我似乎无法弄清如何配置它,使得我的程序在PyCharm中运行。我可以在Mac的终端中成功运行我的程序,但我想在PyCharm中运行它。
我谦卑地向您——互联网上的善良陌生人——寻求帮助。请告诉我在PyCharm中实现我想做的事情是否可能,如果可能,应该如何操作。
在PyCharm的终端窗口中,我收到以下错误:
)07[?47h[1;24r[m[4lTraceback (most recent call last):
File "/rtquote.py", line 24, in <module>
curses.cbreak()
_curses.error: cbreak() returned ERR
Process finished with exit code 1