0

I have some Python Code which works fine the Python IDE and has error handling that seems to work as well. However, when run in Command Line, the code throws up this error:

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>

Here is the code itself:

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")

Does anyone know what this error relates to? I'm fairly new to Python and I haven't managed to find an answer online.

Thanks

user3043997
  • 27
  • 1
  • 7
  • ignore the print("press ENTER to exit") by the way. – user3043997 Mar 11 '14 at 22:19
  • Sorry, yes. Windows Vista. – user3043997 Mar 11 '14 at 22:25
  • 2
    As an unrelated aside, you should really encode that massive elif block as a dict. – aruisdante Mar 11 '14 at 22:26
  • [This](http://stackoverflow.com/questions/14630288/unicodeencodeerror-charmap-codec-cant-encode-character-maps-to-undefined) should get you started. – Drewness Mar 11 '14 at 22:29
  • really he can just change all of his strings that have that funny apostrophe and the issue should be resolved – Joran Beasley Mar 11 '14 at 22:31
  • Ok. I get the basic idea of dictionaries in Python, but I will read that link in more detail tomorrow. Thanks. – user3043997 Mar 11 '14 at 22:58
  • `results = {'a':'Ask','y':'Dividend Yield' ........ 's7':'Short Ratio'}` and then get result by doing `b = results[a]` also try to not use single letter variables it makes code reading more difficult, meaningful names are best. Also since you are grabbing a page and getting contents, why not just parse that info out of the contents. – TehTris Nov 18 '14 at 21:26

2 Answers2

2

this error is because your terminal does not support unicode characters ... and at some-point you are trying to print them (likely page results)

you can

  1. log them to a file opened in binary mode
  2. escape your results with some_text.encode('ascii','replace')

for example

print('b =', b.encode('ascii','replace'))

but the standard windows command terminal does not support unicode characters ... most IDE's do, (IDLE for example)

specifically I believe this is your problem

 b = 'Day’s Value Change'   #<--- use a normal apostrophe not a fancy one
...
 b = 'Day’s Low' # again should be "Day's Low"
...
 etc
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    Basically, Window's terminal as awful. – aruisdante Mar 11 '14 at 22:35
  • On a separate issue I have been looking at building a log into my code,but have only speed read some of the links as it is getting late here in the UK. Do you have any recommended posts/further reading for how to build a log? – user3043997 Mar 11 '14 at 22:59
  • By the way, removing the apostrophe's helped. The code was copied from a webpage into SAS and then out of SAS into Python manually, so that will be where the irregular character came from. Thanks. – user3043997 Mar 11 '14 at 23:01
0

You probably have a 'utf-8' character and you are trying to encode it as ascii. You need to explicitly specify the codec that you need to use.

b = b.encode('utf-8')

This should let you print it.

sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • even then Im pretty sure the windows cmd terminal wont handle it correctly (since its 3 bytes) – Joran Beasley Mar 11 '14 at 22:38
  • @Joran I would say that your answer is better. I have seen other problems caused by the fancy apostrophe as well. Since he is putting it in explicitly, then your advice is the correct one. If it was coming in from outside, then he would have to do a replace. A lot of times the answer to a problem of this type is to say "get a real computer" or "I don't do windoze" (:-) (Sorry, I find it hard to resist the snark when it come to problems caused by the games Windows plays) – sabbahillel Mar 11 '14 at 22:45