0

I want to create list for symbols as below:

output = ['A','AA','AABA'...'ZYNE']

The current output is

[
    {u'name': u'Agilent Technologies Inc.', 
     u'symbol': u'A',
     u'iexId': u'2',
     u'date': u'2018-02-12',
     u'type': u'cs',
     u'isEnabled': True},
    {u'name': u'Alcoa Corporation',
     u'symbol': u'AA',
     u'iexId': u'12042',
     u'date': u'2018-02-12',
     u'type': u'cs',
     u'isEnabled': True},
    {u'name': u'Altaba Inc.',
     u'symbol': u'AABA', 
     u'iexId': u'7653',
     u'date': u'2018-02-12',
     u'type': u'cs',
     u'isEnabled': True},

I want to eliminate the unicode and just extract symbols out from the current list.

My code is as below

import urllib, json
url = "https://api.iextrading.com/1.0/ref-data/symbols"
response = urllib.urlopen(url)
data = json.loads(response.read())

I only manage to get one output as data[0]["symbol"] which returns 'u'A' but not in a complete list I want

Gaurav Bharti
  • 789
  • 1
  • 11
  • 22
bkcollection
  • 793
  • 9
  • 29
  • 1
    I would recommend switching to Python 3. – James Feb 13 '18 at 13:01
  • Possible duplicate of [How to get string objects instead of Unicode from JSON?](https://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-from-json) – Chamath Feb 13 '18 at 13:04
  • @bkollection JSON strings are Unicode. What do you hope to gain by pretending otherwise? – Josh Lee Feb 13 '18 at 13:11

1 Answers1

0

Just use:

output = [item['symbol'] for item in data]

EDIT:

If you know that all the 'symbol' values will be in ASCII just use:

output = [str(item['symbol']) for item in data]

Otherwise:

[item['symbol'].encode('ascii', 'ignore') for item in data]
zipa
  • 24,366
  • 6
  • 30
  • 49