Related
I have a json object like this:
[{'currency_pair': 'UOS_USDT',
'orders': [{'account': 'spot',
'amount': '1282.84',
'create_time': '1655394430',
'create_time_ms': 1655394430129,
'currency_pair': 'UOS_USDT',
'fee': '0',
'fee_currency': 'UOS',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208865523',
'left': '1282.84',
'point_fee': '0',
'price': '0.1949',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394430',
'update_time_ms': 1655394430129}],
'total': 1},
{'currency_pair': 'RMRK_USDT',
'orders': [{'account': 'spot',
'amount': '79.365',
'create_time': '1655394431',
'create_time_ms': 1655394431249,
'currency_pair': 'RMRK_USDT',
'fee': '0',
'fee_currency': 'RMRK',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208877018',
'left': '79.365',
'point_fee': '0',
'price': '2.52',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394431',
'update_time_ms': 1655394431249}],
'total': 1}]
I want to convert it to a dataframe.
The data comes from an api call to a crypto exchange. I converted this to json, using the .json() method. So it's proper json. I have tried:
df = pd.DataFrame(data)
df = pd.DataFrame(data["orders")
df = pd.DataFrame(data["currency_pair"]["orders"])
and every other imaginable path.
I want a df which has as columns ["currency_pair", "amount", "create_time", "price", "side"]
I some times get an error TypeError: list indices must be integers or slices, not str or the df works but the orders object is not unpacked. All help gratefully received. Thank you.
import pandas as pd
data = [{'currency_pair': 'UOS_USDT',
'orders': [{'account': 'spot',
'amount': '1282.84',
'create_time': '1655394430',
'create_time_ms': 1655394430129,
'currency_pair': 'UOS_USDT',
'fee': '0',
'fee_currency': 'UOS',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208865523',
'left': '1282.84',
'point_fee': '0',
'price': '0.1949',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394430',
'update_time_ms': 1655394430129}],
'total': 1},
{'currency_pair': 'RMRK_USDT',
'orders': [{'account': 'spot',
'amount': '79.365',
'create_time': '1655394431',
'create_time_ms': 1655394431249,
'currency_pair': 'RMRK_USDT',
'fee': '0',
'fee_currency': 'RMRK',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208877018',
'left': '79.365',
'point_fee': '0',
'price': '2.52',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394431',
'update_time_ms': 1655394431249}],
'total': 1}]
Use:
df = pd.json_normalize(data, record_path=['orders'])
And keep the columns you need.
It's only one line and it should cover your case since 'currency_pair' that you want is already in the 'orders' dictionary and from what I understand from your data it will always be the same as the 'currency_pair' value outside 'orders. As you said you don't need 'total' too.
Use:
df = pd.json_normalize(data, record_path=['orders'], meta=['currency_pair', 'total'], record_prefix='orders_')
If you want them all
import pandas as pd
data = [{'currency_pair': 'UOS_USDT',
'orders': [{'account': 'spot',
'amount': '1282.84',
'create_time': '1655394430',
'create_time_ms': 1655394430129,
'currency_pair': 'UOS_USDT',
'fee': '0',
'fee_currency': 'UOS',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208865523',
'left': '1282.84',
'point_fee': '0',
'price': '0.1949',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394430',
'update_time_ms': 1655394430129}],
'total': 1},
{'currency_pair': 'RMRK_USDT',
'orders': [{'account': 'spot',
'amount': '79.365',
'create_time': '1655394431',
'create_time_ms': 1655394431249,
'currency_pair': 'RMRK_USDT',
'fee': '0',
'fee_currency': 'RMRK',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208877018',
'left': '79.365',
'point_fee': '0',
'price': '2.52',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394431',
'update_time_ms': 1655394431249}],
'total': 1}]
df = pd.DataFrame(data)
df['amount'] = df.apply( lambda row: row.orders[0]['amount'] , axis=1)
df['create_time'] = df.apply( lambda row: row.orders[0]['create_time'] , axis=1)
df['price'] = df.apply( lambda row: row.orders[0]['price'] , axis=1)
df['side'] = df.apply( lambda row: row.orders[0]['side'] , axis=1)
required_df = df[['currency_pair', 'amount', 'create_time', 'price', 'side']]
required_df
Result:
currency_pair amount create_time price side
0 UOS_USDT 1282.84 1655394430 0.1949 buy
1 RMRK_USDT 79.365 1655394431 2.52 buy
HI, hope this process can help you
#Import pandas library
import pandas as pd
#Your data
data = [{'currency_pair': 'UOS_USDT',
'orders': [{'account': 'spot',
'amount': '1282.84',
'create_time': '1655394430',
'create_time_ms': 1655394430129,
'currency_pair': 'UOS_USDT',
'fee': '0',
'fee_currency': 'UOS',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208865523',
'left': '1282.84',
'point_fee': '0',
'price': '0.1949',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394430',
'update_time_ms': 1655394430129}],
'total': 1},
{'currency_pair': 'RMRK_USDT',
'orders': [{'account': 'spot',
'amount': '79.365',
'create_time': '1655394431',
'create_time_ms': 1655394431249,
'currency_pair': 'RMRK_USDT',
'fee': '0',
'fee_currency': 'RMRK',
'fill_price': '0',
'filled_total': '0',
'gt_discount': False,
'gt_fee': '0',
'iceberg': '0',
'id': '169208877018',
'left': '79.365',
'point_fee': '0',
'price': '2.52',
'rebated_fee': '0',
'rebated_fee_currency': 'USDT',
'side': 'buy',
'status': 'open',
'text': 'apiv4',
'time_in_force': 'gtc',
'type': 'limit',
'update_time': '1655394431',
'update_time_ms': 1655394431249}],
'total': 1}]
#Accessing nested values
#you cloud transform the specific column
#into a DataFrame and access it values with indices
#then parse the value to the type you need
#i.e
float(pd.DataFrame(data[0]['orders'])['amount'].values[0])
int(pd.DataFrame(data[0]['orders'])['create_time'].values[0])
float(pd.DataFrame(data[0]['orders'])['price'].values[0])
pd.DataFrame(data[0]['orders'])['side'].values[0]
#Create a dictionary with your chosen structure
#["currency_pair", "amount", "create_time", "price", "side"]
# then insert the corresponding columns
custom_dictionary = {
'currency_pair': [data[0]['currency_pair'], data[1]['currency_pair']],
'amount': [float(pd.DataFrame(data[0]['orders'])['amount'].values[0]),
float(pd.DataFrame(data[1]['orders'])['amount'].values[0])],
'create_time': [int(pd.DataFrame(data[0]['orders'])['create_time'].values[0]),
int(pd.DataFrame(data[1]['orders'])['create_time'].values[0])],
'price': [float(pd.DataFrame(data[0]['orders'])['price'].values[0]),
float(pd.DataFrame(data[1]['orders'])['price'].values[0])],
'side': [pd.DataFrame(data[0]['orders'])['side'].values[0],
pd.DataFrame(data[1]['orders'])['side'].values[0]]}
#Create a DataFrame with your custom dictionary and voila
df = pd.DataFrame(custom_dictionary)
df
the dataframe (df) could look like:
custom DataFrame
My Code:
import requests
import json
web_page = requests.get("http://api.bart.gov/api/etd.aspx?cmd=etd&orig=mont&key=MW9S-E7SL-26DU-VV8V&json=y")
response = web_page.text
parsed_json = json.loads(response)
#print(parsed_json)
print(parsed_json['root']['date'])
print(parsed_json['root']['time'])
print(parsed_json['root']['station']['name'])
How to extract value of destination and minutes from below in Python.
[{'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'WHITE', 'hexcolor': '#ffffff', 'bikeflag': '1', 'delay': '220'}]}, {'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '16', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '132'}, {'minutes': '26', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '69'}]}]}]
Try this:
json_obj = {'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Antioch', 'abbreviation': 'ANTC', 'limited': '0', 'estimate': [{'minutes': '1', 'platform': '2', 'direction': 'North', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '254'}]},
{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '0', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '0'}]},
{'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '38', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '0'}]}]}
for item in json_obj['etd']:
dest = item['destination']
minute = item['estimate'][0]['minutes']
print(dest, minute)
Output:
Antioch 1
Daly City 39
SF Airport 38
The problem is in parsed_json['root']['station']['name']. parsed_json['root']['station'] is a list, not a dict, so it doesn't have name key. You need to use index 0 or iterate over it
for station in parsed_json['root']['station']:
for etd in station['etd']:
for estimate in etd['estimate']:
print(etd['destination'], estimate['minutes'])
Output
Daly City 35
SF Airport 16
SF Airport 26
Try this to get json data:
import json
# some JSON:
json_data= {'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '0', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '0'}]}
# parse json_data:
data = json.dumps(json_data)
extract_json = json.loads(data)
print("Destination: "+extract_json["destination"])
print("Minutes: "+extract_json["estimate"][0]["minutes"])
Output:
Destination: Daly City
Minutes: 39
Assuming the data is in d_MONT:
d_MONT = {'name': 'Montgomery St.', 'abbr': 'MONT', 'etd': [{'destination': 'Antioch', 'abbreviation': 'ANTC', 'limited': '0', 'estimate': [{'minutes': '1', 'platform': '2', 'direction': 'North', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '254'}]},
{'destination': 'Daly City', 'abbreviation': 'DALY', 'limited': '0', 'estimate': [{'minutes': '39', 'platform': '1', 'direction': 'South', 'length': '0', 'color': 'BLUE', 'hexcolor': '#0099cc', 'bikeflag': '1', 'delay': '0'}]},
{'destination': 'SF Airport', 'abbreviation': 'SFIA', 'limited': '0', 'estimate': [{'minutes': '38', 'platform': '1', 'direction': 'South', 'length': '10', 'color': 'YELLOW', 'hexcolor': '#ffff33', 'bikeflag': '1', 'delay': '0'}]}]}
This will find the next train to destinationRequired:
destinationList = d_MONT['etd']
destinationRequired = 'Daly City'
for destinationDict in destinationList:
if destinationDict['destination'] == destinationRequired:
earliest = None
for estimate in destinationDict['estimate']:
if earliest is None or estimate['minutes'] < eariest:
earliest = estimate['minutes']
print("Next train to {0}: {1} minutes".format(destinationRequired, earliest))
break
else:
print("No trains to {0}".format(destinationRequired))
Note there are more Pythonic ways to do this, and the code example above does not follow PEP8, but I think it is important you understand the basic logic of how to do what you want rather than a complex Python one-liner.
You do not document the JSON object format, so I don't think it is safe to assume the list of trains to destination will be in order, therefore the safest is to step through each one and find the earliest. It isn't even clear if more than one train will ever be returned in the list, in which case a simple [0] would be sufficient rather than stepping through each one.
In my scinario, I am having data as kind of a sorted list:
sample = ['1:4', '2:2', '3:1', '4:1', '5:1', 'B5S94Y:1', 'DB:4', 'ICEauthority:1', 'JPG:1', 'MZPZ5Y:1', 'Mdg80A:1', 'TAG:1', 'V2XO5Y:1', 'Xauthority:1', 'apmrc:1', 'asc:1', 'bak:1', 'baseA:4', 'baseB:4', 'bash:1', 'bash_history:1', 'bash_logout:1', 'bashrc:1', 'bat:1', 'bau:1', 'bdic:1', 'bin:5', 'c:282', 'cache-6:55', 'cache:103', 'cfg:3', 'conf:15', 'converted-launchers:1', 'cson:2', 'css:34', 'dat:2', 'db-journal:3', 'db-shm:1', 'db-wal:1', 'db:17', 'dbf:1', 'dbt:1', 'deb:3', 'desktop:2', 'dic:1', 'dirs:1', 'dll:42', 'dmrc:1', 'docx:9', 'exc:1', 'exe:20', 'final:1', 'fingerprint:4', 'fmt:1', 'gif:5', 'gitconfig:1', 'gitignore:6', 'gitkeep:2', 'gitmodules:1', 'gpg-agent:1', 'gpg:1', 'gradle:3', 'gvdb:1', 'gz:169', 'h:14', 'htm:1', 'html:43', 'icc:1', 'ico:1', 'ics:3', 'idx:5', 'info:1', 'ini:14', 'ino:1', 'isrunning:1', 'jar:1', 'java:7', 'journal:2', 'jpg:16', 'js-20170612122310:1', 'js-20170816210634:1', 'js:245', 'json:367', 'jsonlz4:39', 'kbx:1', 'keyring:1', 'keystore:1', 'ldb:23', 'list:2', 'little:2', 'locale:1', 'localstorage-journal:71', 'localstorage:71', 'lock:1', 'log:41', 'lst:2', 'lsup7I:1', 'lz4:1', 'm:1', 'map:2', 'md:92', 'md~:1', 'metadata-v2:6', 'metadata:6', 'mozlz4:2', 'name:1', 'nls:1', 'odb:2', 'odt:1', 'old:24', 'orig:1', 'otf:31', 'out:19', 'pack:5', 'pak:1', 'parentlock:2', 'pb:32', 'pdf:18', 'pf2:5', 'php:12', 'pl:1', 'pma:2', 'png:2348', 'pro:1', 'profile:1', 'properties:2', 'pset:42', 'pub:1', 'py:41', 'pyc:4', 'rb:1', 'rcache:2', 'rdf:2', 'reg:3', 'run:1', 'sample:72', 'sbstore:42', 'sdv:1', 'sh:27', 'so:2', 'source:1', 'spec:9', 'sqlite-journal:1', 'sqlite-shm:4', 'sqlite-wal:4', 'sqlite3-journal:1', 'sqlite3:1', 'sqlite:24', 'stamp:2', 'stderr:8', 'stdout:8', 'sth:19', 'sublime_session:1', 'sudo_as_admin_successful:1', 'svg:124', 'swo:1', 'swp:2', 'sys:1', 'tdb:3', 'thm:1', 'trashinfo:3', 'tsv:1', 'ttf:107', 'tvc:1', 'txt:63', 'update-timestamp:1', 'usage:6', 'viminfo:1', 'vxd:1', 'woff:1', 'x86_64-pc-linux-gnu:1', 'xba:1', 'xbel:1', 'xcu:1', 'xinputrc:1', 'xlb:2', 'xlc:2', 'xml:35', 'xpi:9', 'xsession-errors:1', 'yml:1', 'zcompdump:1', 'zip:5', 'zsh-template:1', 'zsh-theme:143', 'zsh-update:1', 'zsh:235', 'zsh_history:1', 'zshrc:1']
So after converting this above list to a dictionary with the following code:
dict_val = {a:b for a, b in [i.split(":") for i in sample]}
The output will be:
{'baseA': '4', 'baseB': '4', 'cache-6': '55', 'Xauthority': '1', 'gitmodules': '1', 'apmrc': '1', 'gz': '169', 'dbf': '1', 'kbx': '1', 'sqlite-shm': '4', 'dbt': '1', 'gitignore': '6', 'xml': '35', 'sbstore': '42', 'cache': '103', 'jar': '1', 'desktop': '2', 'source': '1', 'sqlite3-journal': '1', 'TAG': '1', '4': '1', 'tsv': '1', 'spec': '9', 'bin': '5', 'docx': '9', 'woff': '1', 'db': '17', 'V2XO5Y': '1', 'dat': '2', 'fingerprint': '4', 'lz4': '1', 'name': '1', 'bat': '1', 'bau': '1', 'converted-launchers': '1', 'h': '14', 'list': '2', 'lst': '2', 'gradle': '3', 'zsh-update': '1', 'mozlz4': '2', 'stderr': '8', 'sublime_session': '1', 'bak': '1', 'isrunning': '1', 'locale': '1', 'cfg': '3', 'htm': '1', 'odt': '1', 'xlb': '2', 'md~': '1', 'pma': '2', 'sqlite-journal': '1', 'odb': '2', 'dic': '1', 'tvc': '1', 'out': '19', 'ico': '1', 'icc': '1', 'dll': '42', '3': '1', 'rb': '1', 'ics': '3', 'py': '41', 'journal': '2', 'metadata': '6', 'dirs': '1', 'run': '1', 'tdb': '3', 'DB': '4', 'zshrc': '1', 'xpi': '9', 'pub': '1', 'js': '245', 'asc': '1', 'ldb': '23', 'xlc': '2', 'xbel': '1', 'properties': '2', 'bash': '1', 'sys': '1', 'c': '282', 'zip': '5', 'idx': '5', 'lsup7I': '1', 'zcompdump': '1', 'rdf': '2', 'dmrc': '1', 'Mdg80A': '1', 'pdf': '18', 'reg': '3', 'jsonlz4': '39', 'bashrc': '1', 'db-journal': '3', 'pf2': '5', 'localstorage': '71', 'old': '24', 'txt': '63', 'orig': '1', 'gvdb': '1', 'little': '2', 'pyc': '4', 'java': '7', 'log': '41', 'swo': '1', 'stamp': '2', 'vxd': '1', 'fmt': '1', 'gpg': '1', 'zsh-template': '1', 'pb': '32', 'gif': '5', 'json': '367', '2': '2', 'js-20170612122310': '1', 'swp': '2', 'bash_logout': '1', 'final': '1', 'pl': '1', 'gpg-agent': '1', 'sdv': '1', 'x86_64-pc-linux-gnu': '1', 'parentlock': '2', 'cson': '2', 'rcache': '2', 'otf': '31', 'usage': '6', 'bash_history': '1', 'localstorage-journal': '71', 'update-timestamp': '1', 'png': '2348', 'exc': '1', 'info': '1', 'md': '92', 'js-20170816210634': '1', 'sth': '19', 'yml': '1', 'sqlite-wal': '4', 'deb': '3', 'zsh': '235', 'pack': '5', 'zsh_history': '1', 'sqlite': '24', 'stdout': '8', 'lock': '1', 'pro': '1', 'gitkeep': '2', 'jpg': '16', 'sample': '72', 'ino': '1', 'pset': '42', 'ini': '14', 'conf': '15', 'xcu': '1', 'sudo_as_admin_successful': '1', 'xsession-errors': '1', 'keystore': '1', 'nls': '1', 'sh': '27', 'bdic': '1', '1': '4', 'html': '43', '5': '1', 'MZPZ5Y': '1', 'sqlite3': '1', 'pak': '1', 'ttf': '107', 'css': '34', 'profile': '1', 'map': '2', 'metadata-v2': '6', 'm': '1', 'zsh-theme': '143', 'trashinfo': '3', 'ICEauthority': '1', 'php': '12', 'B5S94Y': '1', 'viminfo': '1', 'exe': '20', 'db-shm': '1', 'xinputrc': '1', 'svg': '124', 'keyring': '1', 'JPG': '1', 'thm': '1', 'gitconfig': '1', 'so': '2', 'xba': '1', 'db-wal': '1'}
So I want to know why the order of the output data has changed compared to the input data and how to get the output in the same sorted order as the input is given!!
dicts in python versions older than 3.6 do not have any notion of ordering. If you want a dictionary that maintains order, you'll want the collections.OrderedDict data structure.
from collections import OrderedDict
mapping = OrderedDict(i.split(":") for i in sample)
mapping is an OrderedDict, a subclass of dict which supports all the basic dict functionality, plus the ordering.
Also, do not use dict as a variable name, it shadows the builtin class dict.
This question already has answers here:
Python: how to build a dict from plain list of keys and values
(5 answers)
Closed 5 years ago.
I have a list where the content of list are key:value pairs something like shown below
sample =['ldb:21', 'baseB:4', 'cache-6:55', 'Xauthority:1', 'baseA:4',
'apmrc:1', 'gz:169', 'dbf:1', 'lst:2', 'sqlite-shm:4', 'ttf:107',
'gitignore:6', 'xml:35', 'sbstore:42', 'cache:103', 'jar:1',
'desktop:2', 'source:1', 'sqlite3-journal:1', 'TAG:1', '4:1',
'usage:6', 'yml:1', 'bin:5', 'docx:9', 'woff:1', 'db:17',
'gpg-agent:1', 'V2XO5Y:1', 'dat:2', 'fingerprint:4', 'lz4:1',
'cson:2', 'name:1', 'bat:1', 'bau:1', 'converted-launchers:1',
'h:14', 'list:2', 'xlb:2', 'dic:1', 'zsh-update:1',
'mozlz4:2', 'stderr:8', 'sublime_session:1', 'bak:1', 'dll:42',
'old:24', 'locale:1', 'cfg:3', 'htm:1', 'odt:1', 'keyring:1',
'md~:1', 'pma:2', 'sqlite-journal:1', 'odb:2', 'gradle:3', 'tvc:1',
'out:19', 'ico:1', 'icc:1', 'gpg:1', 'dbt:1', '3:1', 'rb:1',
'ics:3', 'reg:3', 'metadata:6', 'dirs:1', 'run:1', 'tdb:3',
'journal:2', 'zshrc:1', 'little:2', 'pub:1', 'js:245',
'asc:1', 'xbel:1', 'properties:2', 'bash:1', 'sys:1', 'c:282',
'zip:5', 'idx:5', 'lsup7I:1', 'zcompdump:1', 'rdf:2', 'dmrc:1',
'Mdg80A:1', 'pdf:18', 'xlc:2', 'jsonlz4:39', 'bashrc:1',
'db-journal:3', 'pf2:5', 'localstorage:71', 'isrunning:1',
'txt:63', 'orig:1', 'gvdb:1', 'xpi:9', 'php:12',
'gitmodules:1', 'log:41', 'swo:1', 'stamp:2', 'vxd:1',
'fmt:1', 'py:41']
I want to convert the above list into dictionary key:value pairs.
Then I want to convert it into json format.
You can try this:
sample =['ldb:21', 'baseB:4', 'cache-6:55', 'Xauthority:1', 'baseA:4', 'apmrc:1', 'gz:169', 'dbf:1', 'lst:2', 'sqlite-shm:4', 'ttf:107', 'gitignore:6', 'xml:35', 'sbstore:42', 'cache:103', 'jar:1', 'desktop:2', 'source:1', 'sqlite3-journal:1', 'TAG:1', '4:1', 'usage:6', 'yml:1', 'bin:5', 'docx:9', 'woff:1', 'db:17', 'gpg-agent:1', 'V2XO5Y:1', 'dat:2', 'fingerprint:4', 'lz4:1', 'cson:2', 'name:1', 'bat:1', 'bau:1', 'converted-launchers:1', 'h:14', 'list:2', 'xlb:2', 'dic:1', 'zsh-update:1', 'mozlz4:2', 'stderr:8', 'sublime_session:1', 'bak:1', 'dll:42', 'old:24', 'locale:1', 'cfg:3', 'htm:1', 'odt:1', 'keyring:1', 'md~:1', 'pma:2', 'sqlite-journal:1', 'odb:2', 'gradle:3', 'tvc:1', 'out:19', 'ico:1', 'icc:1', 'gpg:1', 'dbt:1', '3:1', 'rb:1', 'ics:3', 'reg:3', 'metadata:6', 'dirs:1', 'run:1', 'tdb:3', 'journal:2', 'zshrc:1', 'little:2', 'pub:1', 'js:245', 'asc:1', 'xbel:1', 'properties:2', 'bash:1', 'sys:1', 'c:282', 'zip:5', 'idx:5', 'lsup7I:1', 'zcompdump:1', 'rdf:2', 'dmrc:1', 'Mdg80A:1', 'pdf:18', 'xlc:2', 'jsonlz4:39', 'bashrc:1', 'db-journal:3', 'pf2:5', 'localstorage:71', 'isrunning:1', 'txt:63', 'orig:1', 'gvdb:1', 'xpi:9', 'php:12', 'gitmodules:1', 'log:41', 'swo:1', 'stamp:2', 'vxd:1', 'fmt:1', 'py:41']
final_data = {a:b for a, b in [i.split(":") for i in sample]}
print(final_data)
Output:
{'ldb': '21', 'baseB': '4', 'cache-6': '55', 'Xauthority': '1', 'baseA': '4', 'apmrc': '1', 'gz': '169', 'dbf': '1', 'lst': '2', 'dll': '42', 'ttf': '107', 'gitignore': '6', 'xml': '35', 'sbstore': '42', 'cache': '103', 'jar': '1', 'desktop': '2', 'source': '1', 'sqlite3-journal': '1', 'TAG': '1', '4': '1', 'usage': '6', 'yml': '1', 'bin': '5', 'docx': '9', 'woff': '1', 'dbt': '1', 'V2XO5Y': '1', 'dat': '2', 'fingerprint': '4', 'lz4': '1', 'name': '1', 'bat': '1', 'bau': '1', 'converted-launchers': '1', 'h': '14', 'list': '2', 'xlb': '2', 'gradle': '3', 'zsh-update': '1', 'stderr': '8', 'sublime_session': '1', 'bak': '1', 'old': '24', 'locale': '1', 'cfg': '3', 'htm': '1', 'odt': '1', 'md~': '1', 'pma': '2', 'sqlite-journal': '1', 'odb': '2', 'dic': '1', 'tvc': '1', 'out': '19', 'ico': '1', 'icc': '1', 'sqlite-shm': '4', '3': '1', 'rb': '1', 'ics': '3', 'py': '41', 'reg': '3', 'metadata': '6', 'dirs': '1', 'run': '1', 'tdb': '3', 'journal': '2', 'zshrc': '1', 'xpi': '9', 'pub': '1', 'js': '245', 'asc': '1', 'xbel': '1', 'properties': '2', 'bash': '1', 'c': '282', 'swo': '1', 'idx': '5', 'lsup7I': '1', 'rdf': '2', 'dmrc': '1', 'Mdg80A': '1', 'pdf': '18', 'xlc': '2', 'jsonlz4': '39', 'bashrc': '1', 'db-journal': '3', 'pf2': '5', 'localstorage': '71', 'isrunning': '1', 'txt': '63', 'orig': '1', 'gvdb': '1', 'little': '2', 'gitmodules': '1', 'log': '41', 'zip': '5', 'stamp': '2', 'vxd': '1', 'fmt': '1', 'gpg': '1', 'gpg-agent': '1', 'cson': '2', 'zcompdump': '1', 'mozlz4': '2', 'db': '17', 'sys': '1', 'php': '12', 'keyring': '1'}
Just use dict, splitting on the colons.
dict(item.split(':') for item in sample)
Or the equivalent functional approach,
from operator import methodcaller
dict(map(methodcaller('split', ':'), sample))
From there json.dumps to get a JSON formatted string.
sample =['ldb:21', 'baseB:4', 'cache-6:55', 'Xauthority:1', 'baseA:4', 'apmrc:1', 'gz:169', 'dbf:1', 'lst:2', 'sqlite-shm:4', 'ttf:107', 'gitignore:6', 'xml:35', 'sbstore:42', 'cache:103', 'jar:1', 'desktop:2', 'source:1', 'sqlite3-journal:1', 'TAG:1', '4:1', 'usage:6', 'yml:1', 'bin:5', 'docx:9', 'woff:1', 'db:17', 'gpg-agent:1', 'V2XO5Y:1', 'dat:2', 'fingerprint:4', 'lz4:1', 'cson:2', 'name:1', 'bat:1', 'bau:1', 'converted-launchers:1', 'h:14', 'list:2', 'xlb:2', 'dic:1', 'zsh-update:1', 'mozlz4:2', 'stderr:8', 'sublime_session:1', 'bak:1', 'dll:42', 'old:24', 'locale:1', 'cfg:3', 'htm:1', 'odt:1', 'keyring:1', 'md~:1', 'pma:2', 'sqlite-journal:1', 'odb:2', 'gradle:3', 'tvc:1', 'out:19', 'ico:1', 'icc:1', 'gpg:1', 'dbt:1', '3:1', 'rb:1', 'ics:3', 'reg:3', 'metadata:6', 'dirs:1', 'run:1', 'tdb:3', 'journal:2', 'zshrc:1', 'little:2', 'pub:1', 'js:245', 'asc:1', 'xbel:1', 'properties:2', 'bash:1', 'sys:1', 'c:282', 'zip:5', 'idx:5', 'lsup7I:1', 'zcompdump:1', 'rdf:2', 'dmrc:1', 'Mdg80A:1', 'pdf:18', 'xlc:2', 'jsonlz4:39', 'bashrc:1', 'db-journal:3', 'pf2:5', 'localstorage:71', 'isrunning:1', 'txt:63', 'orig:1', 'gvdb:1', 'xpi:9', 'php:12', 'gitmodules:1', 'log:41', 'swo:1', 'stamp:2', 'vxd:1', 'fmt:1', 'py:41']
dict_data = {}
for data in sample:
item = data.split(':')
dict_data[item[0]] = item[1]
print dict_data
output:
{'ldb': '21', 'baseB': '4', 'cache-6': '55', 'Xauthority': '1', 'baseA': '4', 'apmrc': '1', 'gz': '169', 'dbf': '1', 'lst': '2', 'dll': '42', 'ttf': '107', 'gitignore': '6', 'xml': '35', 'sbstore': '42', 'cache': '103', 'jar': '1', 'desktop': '2', 'source': '1', 'sqlite3-journal': '1', 'TAG': '1', '4': '1', 'usage': '6', 'yml': '1', 'bin': '5', 'docx': '9', 'woff': '1', 'dbt': '1', 'V2XO5Y': '1', 'dat': '2', 'fingerprint': '4', 'lz4': '1', 'name': '1', 'bat': '1', 'bau': '1', 'converted-launchers': '1', 'h': '14', 'list': '2', 'xlb': '2', 'gradle': '3', 'zsh-update': '1', 'stderr': '8', 'sublime_session': '1', 'bak': '1', 'old': '24', 'locale': '1', 'cfg': '3', 'htm': '1', 'odt': '1', 'md~': '1', 'pma': '2', 'sqlite-journal': '1', 'odb': '2', 'dic': '1', 'tvc': '1', 'out': '19', 'ico': '1', 'icc': '1', 'sqlite-shm': '4', '3': '1', 'rb': '1', 'ics': '3', 'py': '41', 'reg': '3', 'metadata': '6', 'dirs': '1', 'run': '1', 'tdb': '3', 'journal': '2', 'zshrc': '1', 'xpi': '9', 'pub': '1', 'js': '245', 'asc': '1', 'xbel': '1', 'properties': '2', 'bash': '1', 'c': '282', 'swo': '1', 'idx': '5', 'lsup7I': '1', 'rdf': '2', 'dmrc': '1', 'Mdg80A': '1', 'pdf': '18', 'xlc': '2', 'jsonlz4': '39', 'bashrc': '1', 'db-journal': '3', 'pf2': '5', 'localstorage': '71', 'isrunning': '1', 'txt': '63', 'orig': '1', 'gvdb': '1', 'little': '2', 'gitmodules': '1', 'log': '41', 'zip': '5', 'stamp': '2', 'vxd': '1', 'fmt': '1', 'gpg': '1', 'gpg-agent': '1', 'cson': '2', 'zcompdump': '1', 'mozlz4': '2', 'db': '17', 'sys': '1', 'php': '12', 'keyring': '1'}
Iterate over your list and split the pair into two to form a dictionary
dicta = {a:b for a,b in [pair.split(":") for pair in sample]}
[pair.split(":") for pair in sample] gives you the list of lists:
[["lib", "1"], ["baseB", "4"], ... ]
You can put key as string and value as int, as it seems to be count.
dicta = {a:int(b) for a,b in [pair.split(":") for pair in sample]}
old_list = [ ['ID0', 'ID1'], ['4', '8'], ['5', '6'] ]
I want convert list to new list
key = ['id', 'frame', 'length']
new_list = [{'id': 'ID0', 'frame': '4', 'length': '5'}, {'id': 'ID1', 'frame': '8', 'length': '6'}]
Here's a one-line approach:
>>> [{'id':x, 'frame':y, 'length':z} for x,y,z in zip(*old_list)]
[{'length': '5', 'frame': '4', 'id': 'ID0'}, {'length': '6', 'frame': '8', 'id': 'ID1'}]
new_list=[]
for x,y,z in zip(old_list[0],old_list[1], old_list[2]):
dict = {'id' : x, 'frame' : y, 'length': z}
new_list.append(dict)