[英]Code runs fine in Python GUI but throws up error in Command Line
我有一些Python代碼,它可以很好地工作在Python IDE中,並且具有似乎也能正常工作的錯誤處理。 但是,在命令行中運行時,代碼會拋出此錯誤:
b = Traceback <most recent call last>
File "test.py" line 295, in <module>
print('b =', b)
File "C:\Python33\lib\encodings\cp850.py", line 19, in ecnode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position
3: character maps to <undefined>
這是代碼本身:
import concurrent.futures
import urllib.request
import json
URLS = ["http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=a",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=y",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=b",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=d",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=b2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=r1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=b3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=q",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=p",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=o",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=c1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=d1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=c",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=d2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=c6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=t1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=k2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=p2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=c8",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=c3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m7",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=h",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m8",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=k1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=l",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=l1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=t8",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=w1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=w4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=p1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=m2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=g6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=k",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=v",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=k4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=f6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=n",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=k5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=n4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=w",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=s",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=s1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=x",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=v",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=a5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=b6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=k3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=t7",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=a2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=t6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=i5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=l2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=e",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=l3",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=e7",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=v1",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=e8",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=v7",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=e9",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=s6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=b4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=j4",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=p5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=p6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=r",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=r2",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=r5",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=r6",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=r7",
"http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=s7"]
# Retrieve a single page and report the url and contents
def load_url(url, timeout):
conn = urllib.request.urlopen(url, timeout=timeout)
return conn.readall()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
# Start the load operations and mark each future with its URL
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
c = 0
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
a = ''
b = ''
c += 1
a = url[47:]
print('a=', a)
if a == 'a':
b = 'Ask'
elif a == 'y':
b = 'Dividend Yield'
elif a == 'b':
b = 'Bid'
elif a == 'd':
b = 'Dividend per Share'
elif a == 'b2':
b = 'Ask (Realtime)'
elif a == 'r1':
b = 'Dividend Pay Date'
elif a == 'b3':
b = 'Bid (Realtime)'
elif a == 'q':
b = 'Ex-Dividend Date'
elif a == 'p':
b = 'Previous Close'
elif a == 'o':
b = 'Open'
elif a == 'c1':
b = 'Change'
elif a == 'd1':
b = 'Last Trade Date'
elif a == 'c':
b = 'Change & Percent Change'
elif a == 'd2':
b = 'Trade Date'
elif a == 'c6':
b = 'Change (Realtime)'
elif a == 't1':
b = 'Last Trade Time'
elif a == 'k2':
b = 'Change Percent (Realtime)'
elif a == 'p2':
b = 'Change in Percent'
elif a == 'c8':
b = 'After Hours Change (Realtime)'
elif a == 'm5':
b = 'Change From 200 Day Moving Average'
elif a == 'c3':
b = 'Commission'
elif a == 'm6':
b = 'Percent Change From 200 Day Moving Average'
elif a == 'g':
b = 'Day’s Low'
elif a == 'm7':
b = 'Change From 50 Day Moving Average'
elif a == 'h':
b = 'Day’s High'
elif a == 'm8':
b = 'Percent Change From 50 Day Moving Average'
elif a == 'k1':
b = 'Last Trade (Realtime) With Time'
elif a == 'm3':
b = '50 Day Moving Average'
elif a == 'l':
b = 'Last Trade (With Time)'
elif a == 'm4':
b = '200 Day Moving Average'
elif a == 'l1':
b = 'Last Trade (Price Only)'
elif a == 't8':
b = '1 yr Target Price'
elif a == 'w1':
b = 'Day’s Value Change'
elif a == 'g1':
b = 'Holdings Gain Percent'
elif a == 'w4':
b = 'Day’s Value Change (Realtime)'
elif a == 'g3':
b = 'Annualized Gain'
elif a == 'p1':
b = 'Price Paid'
elif a == 'g4':
b = 'Holdings Gain'
elif a == 'm':
b = 'Day’s Range'
elif a == 'g5':
b = 'Holdings Gain Percent (Realtime)'
elif a == 'm2':
b = 'Day’s Range (Realtime)'
elif a == 'g6':
b = 'Holdings Gain (Realtime)'
elif a == 'k':
b = '52 Week High'
elif a == 'v':
b = 'More Info'
elif a == 'j':
b = '52 week Low'
elif a == 'j1':
b = 'Market Capitalization'
elif a == 'j5':
b = 'Change From 52 Week Low'
elif a == 'j3':
b = 'Market Cap (Realtime)'
elif a == 'k4':
b = 'Change From 52 week High'
elif a == 'f6':
b = 'Float Shares'
elif a == 'j6':
b = 'Percent Change From 52 week Low'
elif a == 'n':
b ='Name'
elif a == 'k5':
b = 'Percent Change From 52 week High'
elif a == 'n4':
b = 'Notes'
elif a == 'w':
b = '52 week Range'
elif a == 's':
b = 'Symbol'
elif a == 's1':
b = 'Shares Owned'
elif a == 'x':
b = 'Stock Exchange'
elif a == 'j2':
b = 'Shares Outstanding'
elif a == 'v':
b = 'Volume'
elif a == 'a5':
b = 'Ask Size'
elif a == 'b6':
b = 'Bid Size'
elif a == 'k3':
b = 'Last Trade Size'
elif a == 't7':
b = 'Ticker Trend'
elif a == 'a2':
b = 'Average Daily Volume'
elif a == 't6':
b = 'Trade Links'
elif a == 'i5':
b = 'Order Book (Realtime)'
elif a == 'l2':
b = 'High Limit'
elif a == 'e':
b = 'Earnings per Share'
elif a == 'l3':
b = 'Low Limit'
elif a == 'e7':
b = 'EPS Estimate Current Year'
elif a == 'v1':
b = 'Holdings Value'
elif a == 'e8':
b = 'EPS Estimate Next Year'
elif a == 'v7':
b = 'Holdings Value (Realtime)'
elif a == 'e9':
b = 'EPS Estimate Next Quarter'
elif a == 's6':
b = 'Revenue'
elif a == 'b4':
b = 'Book Value'
elif a == 'j4':
b = 'EBITDA'
elif a == 'p5':
b = 'Price-Sales'
elif a == 'p6':
b = 'Price-Book'
elif a == 'r':
b = 'P-E Ratio'
elif a == 'r2':
b = 'P-E Ratio (Realtime)'
elif a == 'r5':
b = 'PEG Ratio'
elif a == 'r6':
b = 'Price - EPS Estimate Current Year'
elif a == 'r7':
b = 'Price - EPS Estimate Next Year'
elif a == 's7':
b = 'Short Ratio'
print('b =', b)
print('c =', c)
filename = "%s" % (b)
filepath = "C:\\Python33\\Stock Data\\" + str(filename) + ".txt"
try:
data = future.result()
d = open(filepath,"wb")
d.write(data)
d.close
# do json processing here
except Exception as exc:
for e in range(1,11):
if len(data) > 0:
print('Byte length = %d' % (len(data)))
print(e)
print('%r generated an exception: %s' % (url, exc))
print('retrying %r' % (url))
def load_url(url, timeout):
conn = urllib.request.urlopen(url, timeout=timeout)
return conn.readall()
time.sleep(10)
print("press ENTER to exit")
else:
print('%r page is %d bytes' % (url, len(data)))
print("press ENTER to exit")
有誰知道這個錯誤與什么有關? 我是Python的新手,我還沒有在網上找到答案。
謝謝
此錯誤是因為您的終端不支持unicode字符...並且在某些時候您嘗試打印它們(可能是頁面結果)
您可以
some_text.encode('ascii','replace')
逃避你的結果 例如
print('b =', b.encode('ascii','replace'))
但標准的Windows命令終端不支持unicode字符...大多數IDE都支持,(例如IDLE)
我特別相信這是你的問題
b = 'Day’s Value Change' #<--- use a normal apostrophe not a fancy one
...
b = 'Day’s Low' # again should be "Day's Low"
...
etc
您可能有一個'utf-8'字符,並且您正在嘗試將其編碼為ascii。 您需要明確指定需要使用的編解碼器。
b = b.encode('utf-8')
這應該讓你打印出來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.