I have a JSON file in Python. File contents are below.
{
"cities": [
"NY",
"SFO",
"LA",
"NJ"
],
"companies": [
"Apple",
"Samsung",
"Walmart"
],
"devices": [
"iphone",
"ipad",
"ipod",
"watch"
]
}
I want to create Python lists from this JSON file. I have done like below.
# Open JSON file in Python
with open('test.json') as out_file:
test_data = json.load(out_file)
# Query the output variable test_data
test_data
{u'cities': [u'NY', u'SFO', u'LA', u'NJ'], u'companies': [u'Apple', u'Samsung', u'Walmart'], u'devices': [u'iphone', u'ipad', u'ipod', u'watch']}
# find type of test_data
type(test_data)
<type 'dict'>
# create list from test_data
device = test_data['devices']
# Check content of list created
device
[u'iphone', u'ipad', u'ipod', u'watch']
Now as you see the list is a unicode list I want it to be a pure Python list.
I can do like below
device_list = [str(x) for x in device]
device_list
['iphone', 'ipad', 'ipod', 'watch']
Is there a better way to do this?
unicodeobjects instead ofstrobjects?