Invalid JSON Data From requests.get - python

I am querying a whole-house power monitor (Neurio) that returns data in JSON format.
When I enter the URL in a Chrome browser, http://192.168.1.87/current-sample, I get properly formatted JSON data as follows:
{"sensorId":"0x0000C47F51019B7D","timestamp":"2016-12-24T14:56:08Z","channels":[{"type":"PHASE_A_CONSUMPTION","ch":1,"eImp_Ws":55552784178,"eExp_Ws":23,"p_W":3188,"q_VAR":321,"v_V":121.753},{"type":"PHASE_B_CONSUMPTION","ch":2,"eImp_Ws":62493402411,"eExp_Ws":23,"p_W":3499,"q_VAR":263,"v_V":120.334},{"type":"CONSUMPTION","ch":3,"eImp_Ws":118046186640,"eExp_Ws":41,"p_W":6687,"q_VAR":584,"v_V":121.044}],"cts":[{"ct":1,"p_W":3188,"q_VAR":321,"v_V":121.753},{"ct":2,"p_W":3499,"q_VAR":263,"v_V":120.334},{"ct":3,"p_W":0,"q_VAR":0,"v_V":0.000},{"ct":4,"p_W":0,"q_VAR":0,"v_V":121.747}]}
which parses correctly in JSONLint.
When I attempt to pull the same data in Python using the following line of code:
pvdata = requests.get('http://'+neurioip+'/current-sample').json()
The returned data includes invalid characters as in the following example. (Data retrieved by simply printing pvdata)
{u'channels': [{u'eExp_Ws': 23, u'v_V': 122.434, u'ch': 1, u'eImp_Ws': 55554346060, u'q_VAR': 305, u'p_W': 1489, u'type': u'PHASE_A_CONSUMPTION'}, {u'eExp_Ws': 23, u'v_V': 120.981, u'ch': 2, u'eImp_Ws': 62495160471, u'q_VAR': 237, u'p_W': 1872, u'type': u'PHASE_B_CONSUMPTION'}, {u'eExp_Ws': 41, u'v_V': 121.708, u'ch': 3, u'eImp_Ws': 118049506582, u'q_VAR': 542, u'p_W': 3360, u'type': u'CONSUMPTION'}], u'sensorId': u'0x0000C47F51019B7D', u'cts': [{u'p_W': 1489, u'q_VAR': 305, u'v_V': 122.434, u'ct': 1}, {u'p_W': 1872, u'q_VAR': 237, u'v_V': 120.981, u'ct': 2}, {u'p_W': 0, u'q_VAR': 0, u'v_V': 0.0, u'ct': 3}, {u'p_W': 0, u'q_VAR': 0, u'v_V': 122.432, u'ct': 4}], u'timestamp': u'2016-12-24T15:03:42Z'}
Data retreived using the following code:
for keys,values in pvdata.items():
print(keys)
print(values)
channels
[{u'eExp_Ws': 23, u'v_V': 122.843, u'ch': 1, u'eImp_Ws': 55555370977, u'q_VAR': 14, u'p_W': 230, u'type': u'PHASE_A_CONSUMPTION'}, {u'eExp_Ws': 23, u'v_V': 121.088, u'ch': 2, u'eImp_Ws': 62496733790, u'q_VAR': -3, u'p_W': 661, u'type': u'PHASE_B_CONSUMPTION'}, {u'eExp_Ws': 41, u'v_V': 121.965, u'ch': 3, u'eImp_Ws': 118052104817, u'q_VAR': 12, u'p_W': 890, u'type': u'CONSUMPTION'}]
sensorId
0x0000C47F51019B7D
cts
[{u'p_W': 230, u'q_VAR': 14, u'v_V': 122.843, u'ct': 1}, {u'p_W': 661, u'q_VAR': -3, u'v_V': 121.088, u'ct': 2}, {u'p_W': 0, u'q_VAR': 0, u'v_V': 0.0, u'ct': 3}, {u'p_W': 0, u'q_VAR': 0, u'v_V': 122.84, u'ct': 4}]
timestamp
2016-12-24T15:25:16Z
The "u" characters makes this unparsable in JSONlint or in subsequent lines of code in my program.
I've looked at default character encoding in my Python environment but that doesn't seem to lead anywhere. I'm looking for other ideas to investigate.

.json() is what parses the JSON. It's done. You have a python dictionary which you can use normally now (as you've already seen by iterating through it).

It is unparsable since the string representation of a dict simply isn't JSON; you need to remarshall the dict to JSON if that's what you need:
In [36]: d = {u'channels': [{u'eExp_Ws': 23, u'v_V': 122.434, u'ch': 1, u'eImp_Ws': 55554346060, u'q_VAR': 305, u'p_W': 1489, u'type': u'PHASE_A_CONSUMPTION'}, {u'eExp_Ws': 23, u'v_V': 120.981, u'ch': 2, u'eImp_Ws': 62495160471, u'q_VAR': 237, u'p_W': 1872, u'type': u'PHASE_B_CONSUMPTION'}, {u'eExp_Ws': 41, u'v_V': 121.708, u'ch': 3, u'eImp_Ws': 118049506582, u'q_VAR': 542, u'p_W': 3360, u'type': u'CONSUMPTION'}], u'sensorId': u'0x0000C47F51019B7D', u'cts': [{u'p_W': 1489, u'q_VAR': 305, u'v_V': 122.434, u'ct': 1}, {u'p_W': 1872, u'q_VAR': 237, u'v_V': 120.981, u'ct': 2}, {u'p_W': 0, u'q_VAR': 0, u'v_V': 0.0, u'ct': 3}, {u'p_W': 0, u'q_VAR': 0, u'v_V': 122.432, u'ct': 4}], u'timestamp': u'2016-12-24T15:03:42Z'}
In [38]: json.dumps(d)
Out[38]: '{"sensorId": "0x0000C47F51019B7D", "timestamp": "2016-12-24T15:03:42Z", "cts": [{"q_VAR": 305, "ct": 1, "v_V": 122.434, "p_W": 1489}, {"q_VAR": 237, "ct": 2, "v_V": 120.981, "p_W": 1872}, {"q_VAR": 0, "ct": 3, "v_V": 0.0, "p_W": 0}, {"q_VAR": 0, "ct": 4, "v_V": 122.432, "p_W": 0}], "channels": [{"q_VAR": 305, "type": "PHASE_A_CONSUMPTION", "p_W": 1489, "v_V": 122.434, "ch": 1, "eImp_Ws": 55554346060, "eExp_Ws": 23}, {"q_VAR": 237, "type": "PHASE_B_CONSUMPTION", "p_W":
1872, "v_V": 120.981, "ch": 2, "eImp_Ws": 62495160471, "eExp_Ws": 23}, {"q_VAR": 542, "type": "CONSUMPTION", "p_W": 3360, "v_V": 121.708, "ch": 3, "eImp_Ws": 118049506582, "eExp_Ws": 41}]}'

Related

How to sort players to teams

I have a json with players and teams. They are completly random like:
Now, i want to sort them based on their "team".
I've read the docs and I'm completely confused and don't understand much.
{'puuid': '6bec3c19-6150-57e8-ba84-16220ab49198', 'name': 'Navis', 'tag': 'Games', 'team': 'Red', 'level': 110, 'character': 'Chamber', 'currenttier': 12, 'currenttier_patched': 'Gold 1', 'player_card': '1b6c643b-4f7e-aed8-88f6-f3932e682c14', 'player_title': 'a030f8ac-4475-d8d0-53a1-b8aafd5a3546', 'party_id': '578c7ad5-0eb1-4e6e-ab99-ecf971902cab', 'session_playtime': {'minutes': 231, 'seconds': 13860, 'milliseconds': 13860000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19043.1.256.64bit'}}, 'ability_casts': {'c_cast': 14, 'q_cast': 3, 'e_cast': 1, 'x_cast': 2}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/1b6c643b-4f7e-aed8-88f6-f3932e682c14/smallart.png', 'large': 'https://media.valorant-api.com/playercards/1b6c643b-4f7e-aed8-88f6-f3932e682c14/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/1b6c643b-4f7e-aed8-88f6-f3932e682c14/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/killfeedportrait.png'}}, 'stats': {'score': 1895, 'kills': 7, 'deaths': 15, 'assists': 2, 'bodyshots': 33, 'headshots': 1, 'legshots': 1}, 'economy': {'spent': {'overall': 43150, 'average': 2697}, 'loadout_value': {'overall': 52850, 'average': 3303}}, 'damage_made': 1114, 'damage_received': 2507}
{'puuid': '1446ff0a-9c8a-5cc6-af55-81bf6612a99b', 'name': 'Taha', 'tag': 'Yoru', 'team': 'Red', 'level': 95, 'character': 'Neon', 'currenttier': 13, 'currenttier_patched': 'Gold 2', 'player_card': '5b025468-4810-96e1-d19d-2ea35074586c', 'player_title': 'd13e579c-435e-44d4-cec2-6eae5a3c5ed4', 'party_id': '9d856301-1397-4e6a-b580-cff409a67ab1', 'session_playtime': {'minutes': 154, 'seconds': 9240, 'milliseconds': 9240000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.22000.1.256.64bit'}}, 'ability_casts': {'c_cast': 3, 'q_cast': 0, 'e_cast': 15, 'x_cast': 0}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/5b025468-4810-96e1-d19d-2ea35074586c/smallart.png', 'large': 'https://media.valorant-api.com/playercards/5b025468-4810-96e1-d19d-2ea35074586c/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/5b025468-4810-96e1-d19d-2ea35074586c/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/bb2a4828-46eb-8cd1-e765-15848195d751/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/bb2a4828-46eb-8cd1-e765-15848195d751/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/bb2a4828-46eb-8cd1-e765-15848195d751/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/bb2a4828-46eb-8cd1-e765-15848195d751/killfeedportrait.png'}}, 'stats': {'score': 1132, 'kills': 4, 'deaths': 16, 'assists': 1, 'bodyshots': 15, 'headshots': 5, 'legshots': 0}, 'economy': {'spent': {'overall': 40200, 'average': 2513}, 'loadout_value': {'overall': 45900, 'average': 2869}}, 'damage_made': 542, 'damage_received': 2326}
{'puuid': 'be2b3d3a-7281-5fb8-9b9a-61475111a8b0', 'name': 'YourLocalSkye', 'tag': 'M1lf', 'team': 'Blue', 'level': 99, 'character': 'Chamber', 'currenttier': 15, 'currenttier_patched': 'Platinum 1', 'player_card': '22a21e81-4b57-061f-d56f-aea3c4a8bf33', 'player_title': '631f4283-48b1-1855-d646-5e8f80e29821', 'party_id': 'c0ca03d6-dbf4-495a-9444-a50da9b2b5b3', 'session_playtime': {'minutes': 12, 'seconds': 720, 'milliseconds': 720000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.256.64bit'}}, 'ability_casts': {'c_cast': 14, 'q_cast': 5, 'e_cast': 38, 'x_cast': 1}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/22a21e81-4b57-061f-d56f-aea3c4a8bf33/smallart.png', 'large': 'https://media.valorant-api.com/playercards/22a21e81-4b57-061f-d56f-aea3c4a8bf33/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/22a21e81-4b57-061f-d56f-aea3c4a8bf33/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/22697a3d-45bf-8dd7-4fec-84a9e28c69d7/killfeedportrait.png'}}, 'stats': {'score': 4402, 'kills': 18, 'deaths': 10, 'assists': 3, 'bodyshots': 23, 'headshots': 11, 'legshots': 1}, 'economy': {'spent': {'overall': 35200, 'average': 2200}, 'loadout_value': {'overall': 55100, 'average': 3444}}, 'damage_made': 2724, 'damage_received': 1904}
{'puuid': 'c3a09504-4c30-520a-b1c6-00b95a35a127', 'name': 'FAST TECH YT', 'tag': '11111', 'team': 'Red', 'level': 159, 'character': 'KAY/O', 'currenttier': 14, 'currenttier_patched': 'Gold 3', 'player_card': 'f67a7c8f-4d3f-b76f-2921-478a4da44109', 'player_title': 'a030f8ac-4475-d8d0-53a1-b8aafd5a3546', 'party_id': '563d54bf-b56e-4c17-a771-cb69c02ab164', 'session_playtime': {'minutes': 2, 'seconds': 120, 'milliseconds': 120000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19042.1.256.64bit'}}, 'ability_casts': {'c_cast': 3, 'q_cast': 7, 'e_cast': 21, 'x_cast': 2}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/f67a7c8f-4d3f-b76f-2921-478a4da44109/smallart.png', 'large': 'https://media.valorant-api.com/playercards/f67a7c8f-4d3f-b76f-2921-478a4da44109/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/f67a7c8f-4d3f-b76f-2921-478a4da44109/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/601dbbe7-43ce-be57-2a40-4abd24953621/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/601dbbe7-43ce-be57-2a40-4abd24953621/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/601dbbe7-43ce-be57-2a40-4abd24953621/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/601dbbe7-43ce-be57-2a40-4abd24953621/killfeedportrait.png'}}, 'stats': {'score': 4180, 'kills': 14, 'deaths': 15, 'assists': 4, 'bodyshots': 41, 'headshots': 11, 'legshots': 1}, 'economy': {'spent': {'overall': 39200, 'average': 2450}, 'loadout_value': {'overall': 45850, 'average': 2866}}, 'damage_made': 2838, 'damage_received': 2466}
{'puuid': 'fd976800-eaa4-50ab-8d77-f2ba11ee2334', 'name': 'twitch otisvn', 'tag': 'EUW', 'team': 'Blue', 'level': 87, 'character': 'Viper', 'currenttier': 12, 'currenttier_patched': 'Gold 1', 'player_card': 'eac64718-4956-31ef-2c95-8b85e93d7fcc', 'player_title': 'd13e579c-435e-44d4-cec2-6eae5a3c5ed4', 'party_id': '9fff883f-0eb4-4c67-8621-f2f4fbf7d4cc', 'session_playtime': {'minutes': 4, 'seconds': 240, 'milliseconds': 240000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.768.64bit'}}, 'ability_casts': {'c_cast': 4, 'q_cast': 3, 'e_cast': 15, 'x_cast': 2}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/eac64718-4956-31ef-2c95-8b85e93d7fcc/smallart.png', 'large': 'https://media.valorant-api.com/playercards/eac64718-4956-31ef-2c95-8b85e93d7fcc/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/eac64718-4956-31ef-2c95-8b85e93d7fcc/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/707eab51-4836-f488-046a-cda6bf494859/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/707eab51-4836-f488-046a-cda6bf494859/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/707eab51-4836-f488-046a-cda6bf494859/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/707eab51-4836-f488-046a-cda6bf494859/killfeedportrait.png'}}, 'stats': {'score': 3051, 'kills': 14, 'deaths': 6, 'assists': 5, 'bodyshots': 31, 'headshots': 7, 'legshots': 5}, 'economy': {'spent': {'overall': 23700, 'average': 1481}, 'loadout_value': {'overall': 53300, 'average': 3331}}, 'damage_made': 1836, 'damage_received': 1093}
{'puuid': '4bc6d87f-7a7f-5fa2-a194-b3550e7768da', 'name': 'Sasha', 'tag': '4444', 'team': 'Blue', 'level': 72, 'character': 'Raze', 'currenttier': 14, 'currenttier_patched': 'Gold 3', 'player_card': '4dce4142-44bf-32e5-6f51-7e905389502c', 'player_title': '86bd997d-4e37-18c9-ada0-bf92e54baf60', 'party_id': '88353d38-c1bf-48a1-b943-ecced68711d4', 'session_playtime': {'minutes': 85, 'seconds': 5100, 'milliseconds': 5100000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.256.64bit'}}, 'ability_casts': {'c_cast': 12, 'q_cast': 7, 'e_cast': 8, 'x_cast': 1}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/4dce4142-44bf-32e5-6f51-7e905389502c/smallart.png', 'large': 'https://media.valorant-api.com/playercards/4dce4142-44bf-32e5-6f51-7e905389502c/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/4dce4142-44bf-32e5-6f51-7e905389502c/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/f94c3b30-42be-e959-889c-5aa313dba261/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/f94c3b30-42be-e959-889c-5aa313dba261/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/f94c3b30-42be-e959-889c-5aa313dba261/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/f94c3b30-42be-e959-889c-5aa313dba261/killfeedportrait.png'}}, 'stats': {'score': 5759, 'kills': 17, 'deaths': 11, 'assists': 9, 'bodyshots': 54, 'headshots': 10, 'legshots': 7}, 'economy': {'spent': {'overall': 50750, 'average': 3172}, 'loadout_value': {'overall': 67900, 'average': 4244}}, 'damage_made': 3512, 'damage_received': 2096}
{'puuid': 'd0a6c5fa-5094-5650-9b56-8154d3191725', 'name': 'EnzoNezo', 'tag': '3319', 'team': 'Blue', 'level': 101, 'character': 'Sage', 'currenttier': 12, 'currenttier_patched': 'Gold 1', 'player_card': '5b025468-4810-96e1-d19d-2ea35074586c', 'player_title': '5bdea980-4397-a1bd-1fab-659153ca196e', 'party_id': '33ca0f84-e3b0-4d28-8ae6-f6cda948db72', 'session_playtime': {'seconds': None, 'milliseconds': None}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.256.64bit'}}, 'ability_casts': {'c_cast': 8, 'q_cast': 9, 'e_cast': 4, 'x_cast': 1}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/5b025468-4810-96e1-d19d-2ea35074586c/smallart.png', 'large': 'https://media.valorant-api.com/playercards/5b025468-4810-96e1-d19d-2ea35074586c/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/5b025468-4810-96e1-d19d-2ea35074586c/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/569fdd95-4d10-43ab-ca70-79becc718b46/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/569fdd95-4d10-43ab-ca70-79becc718b46/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/569fdd95-4d10-43ab-ca70-79becc718b46/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/569fdd95-4d10-43ab-ca70-79becc718b46/killfeedportrait.png'}}, 'stats': {'score': 2738, 'kills': 8, 'deaths': 12, 'assists': 12, 'bodyshots': 39, 'headshots': 5, 'legshots': 19}, 'economy': {'spent': {'overall': 54150, 'average': 3384}, 'loadout_value': {'overall': 66200, 'average': 4138}}, 'damage_made': 1591, 'damage_received': 2199}
{'puuid': '70f7373a-ba78-5c9a-a963-767fbf27d9ad', 'name': 'Road2Radaint', 'tag': '6667', 'team': 'Red', 'level': 22, 'character': 'Jett', 'currenttier': 12, 'currenttier_patched': 'Gold 1', 'player_card': '9fb348bc-41a0-91ad-8a3e-818035c4e561', 'player_title': 'dd66cdcb-46cb-5e6e-f66f-51aa8cc3c14b', 'party_id': '578c7ad5-0eb1-4e6e-ab99-ecf971902cab', 'session_playtime': {'minutes': 2, 'seconds': 120, 'milliseconds': 120000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.256.64bit'}}, 'ability_casts': {'c_cast': 6, 'q_cast': 2, 'e_cast': 16, 'x_cast': 2}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/9fb348bc-41a0-91ad-8a3e-818035c4e561/smallart.png', 'large': 'https://media.valorant-api.com/playercards/9fb348bc-41a0-91ad-8a3e-818035c4e561/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/9fb348bc-41a0-91ad-8a3e-818035c4e561/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/add6443a-41bd-e414-f6ad-e58d267f4e95/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/add6443a-41bd-e414-f6ad-e58d267f4e95/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/add6443a-41bd-e414-f6ad-e58d267f4e95/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/add6443a-41bd-e414-f6ad-e58d267f4e95/killfeedportrait.png'}}, 'stats': {'score': 4271, 'kills': 12, 'deaths': 15, 'assists': 6, 'bodyshots': 32, 'headshots': 11, 'legshots': 3}, 'economy': {'spent': {'overall': 35100, 'average': 2194}, 'loadout_value': {'overall': 50900, 'average': 3181}}, 'damage_made': 2682, 'damage_received': 2678}
{'puuid': '1ccd31a8-6109-5edd-bd58-bb90f3b55a1d', 'name': 'E S Q U I', 'tag': '1234', 'team': 'Red', 'level': 61, 'character': 'Fade', 'currenttier': 12, 'currenttier_patched': 'Gold 1', 'player_card': '39729022-445a-d7bb-52e7-9e909e481db2', 'player_title': '025b0aaf-4bc6-522c-faae-a484779c5d03', 'party_id': '51217228-3efc-42f5-9aac-9ef20835a09b', 'session_playtime': {'minutes': 83, 'seconds': 4980, 'milliseconds': 4980000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.256.64bit'}}, 'ability_casts': {'c_cast': 10, 'q_cast': 6, 'e_cast': 14, 'x_cast': 2}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/39729022-445a-d7bb-52e7-9e909e481db2/smallart.png', 'large': 'https://media.valorant-api.com/playercards/39729022-445a-d7bb-52e7-9e909e481db2/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/39729022-445a-d7bb-52e7-9e909e481db2/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/dade69b4-4f5a-8528-247b-219e5a1facd6/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/dade69b4-4f5a-8528-247b-219e5a1facd6/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/dade69b4-4f5a-8528-247b-219e5a1facd6/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/dade69b4-4f5a-8528-247b-219e5a1facd6/killfeedportrait.png'}}, 'stats': {'score': 2287, 'kills': 8, 'deaths': 15, 'assists': 0, 'bodyshots': 17, 'headshots': 4, 'legshots': 2}, 'economy': {'spent': {'overall': 43000, 'average': 2688}, 'loadout_value': {'overall': 47750, 'average': 2984}}, 'damage_made': 1382, 'damage_received': 2424}
{'puuid': '55ac8119-1247-517d-85fb-5134a957d8df', 'name': 'Menzirs', 'tag': '9042', 'team': 'Blue', 'level': 81, 'character': 'Skye', 'currenttier': 14, 'currenttier_patched': 'Gold 3', 'player_card': '4ca63988-4ca6-2911-a1b5-98a4b765dffd', 'player_title': '39374ca0-447b-1d4e-aa38-e6b0e0b07946', 'party_id': '5c59434c-7a64-4736-956c-e35038aaabeb', 'session_playtime': {'minutes': 183, 'seconds': 10980, 'milliseconds': 10980000}, 'behavior': {'afk_rounds': 0, 'friendly_fire': {'incoming': 0, 'outgoing': 0}, 'rounds_in_spawn': 0}, 'platform': {'type': 'PC', 'os': {'name': 'Windows', 'version': '10.0.19044.1.768.64bit'}}, 'ability_casts': {'c_cast': 4, 'q_cast': 5, 'e_cast': 26, 'x_cast': 3}, 'assets': {'card': {'small': 'https://media.valorant-api.com/playercards/4ca63988-4ca6-2911-a1b5-98a4b765dffd/smallart.png', 'large': 'https://media.valorant-api.com/playercards/4ca63988-4ca6-2911-a1b5-98a4b765dffd/largeart.png', 'wide': 'https://media.valorant-api.com/playercards/4ca63988-4ca6-2911-a1b5-98a4b765dffd/wideart.png'}, 'agent': {'small': 'https://media.valorant-api.com/agents/6f2a04ca-43e0-be17-7f36-b3908627744d/displayicon.png', 'bust': 'https://media.valorant-api.com/agents/6f2a04ca-43e0-be17-7f36-b3908627744d/bustportrait.png', 'full': 'https://media.valorant-api.com/agents/6f2a04ca-43e0-be17-7f36-b3908627744d/fullportrait.png', 'killfeed': 'https://media.valorant-api.com/agents/6f2a04ca-43e0-be17-7f36-b3908627744d/killfeedportrait.png'}}, 'stats': {'score': 4217, 'kills': 19, 'deaths': 6, 'assists': 7, 'bodyshots': 35, 'headshots': 12, 'legshots': 0}, 'economy': {'spent': {'overall': 32700, 'average': 2044}, 'loadout_value': {'overall': 67900, 'average': 4244}}, 'damage_made': 2573, 'damage_received': 1422}
This is only a part of the JSON which is neccessary to my Question. The other part of the JSON is way longer.
Use the sorted function, with the key as the 'team' value.
data = json.load(open('filepath.json'))
result = sorted(data, key=lambda x: x['team'])

Python barplot from Counter

I am brand new to Python and I am starting a BS in data analytics in August I am trying to get a head start on learning. Can anyone solve this for me?
from collections import Counter
Counter(one_d)
returns the following
Counter({'Action': 303,
'Adventure': 259,
'Sci-Fi': 120,
'Mystery': 106,
'Horror': 119,
'Thriller': 195,
'Animation': 49,
'Comedy': 279,
'Family': 51,
'Fantasy': 101,
'Drama': 513,
'Music': 16,
'Biography': 81,
'Romance': 141,
'History': 29,
'Crime': 150,
'Western': 7,
'War': 13,
'Musical': 5,
'Sport': 18})
I would like to create a Barplot but am unsure how to do this. Is barplot even the best choice for this data?
The Pandas library is quite useful for data analytics and visualization:
from collections import Counter
import pandas as pd
counts = Counter({'Action': 303, 'Adventure': 259, 'Sci-Fi': 120, 'Mystery': 106, 'Horror': 119, 'Thriller': 195, 'Animation': 49, 'Comedy': 279, 'Family': 51, 'Fantasy': 101, 'Drama': 513, 'Music': 16, 'Biography': 81, 'Romance': 141, 'History': 29, 'Crime': 150, 'Western': 7, 'War': 13, 'Musical': 5, 'Sport': 18})
data = pd.Series(counts)
ax = data.plot.bar()
ax.set(xlabel='Genre', ylabel='Count', title='Good luck on your BS')

Iterating through JSON and appending into dataframe

I'm getting weather forecasting data from weatherstack API.
params = {
'access_key': 'enter_key_here',
'query': 'Montreal',
'forecast_days': '2', #two days forecast
'hourly': '1' #API to return weather data split hourly
}
api_result = requests.get('https://api.weatherstack.com/forecast', params)
api_response = api_result.json()
api_response
The output looks like this:
{'request': {'type': 'City',
'query': 'Montreal, Canada',
'language': 'en',
'unit': 'm'},
'location': {'name': 'Montreal',
'country': 'Canada',
'region': 'Quebec',
'lat': '45.500',
'lon': '-73.583',
'timezone_id': 'America/Toronto',
'localtime': '2021-05-11 13:08',
'localtime_epoch': 1620738480,
'utc_offset': '-4.0'},
'current': {'observation_time': '05:08 PM',
'temperature': 11,
'weather_code': 122,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Overcast'],
'wind_speed': 31,
'wind_degree': 230,
'wind_dir': 'SW',
'pressure': 1012,
'precip': 1,
'humidity': 58,
'cloudcover': 100,
'feelslike': 8,
'uv_index': 2,
'visibility': 14,
'is_day': 'yes'},
'forecast': {'2021-05-11': {'date': '2021-05-11',
'date_epoch': 1620691200,
'astro': {'sunrise': '05:28 AM',
'sunset': '08:14 PM',
'moonrise': '05:35 AM',
'moonset': '08:14 PM',
'moon_phase': 'Waxing Crescent',
'moon_illumination': 7},
'mintemp': 6,
'maxtemp': 13,
'avgtemp': 8,
'totalsnow': 0,
'sunhour': 12.4,
'uv_index': 6,
'hourly': [{'time': '0',
'temperature': 9,
'wind_speed': 16,
'wind_degree': 240,
'wind_dir': 'WSW',
'weather_code': 119,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png'],
'weather_descriptions': ['Cloudy'],
'precip': 0.1,
'humidity': 74,
'visibility': 10,
'pressure': 1012,
'cloudcover': 75,
'heatindex': 9,
'dewpoint': 5,
'windchill': 7,
'windgust': 23,
'feelslike': 7,
'chanceofrain': 43,
'chanceofremdry': 31,
'chanceofwindy': 0,
'chanceofovercast': 86,
'chanceofsunshine': 5,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 1},
{'time': '300',
'temperature': 8,
'wind_speed': 15,
'wind_degree': 247,
'wind_dir': 'WSW',
'weather_code': 122,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Overcast'],
'precip': 0,
'humidity': 83,
'visibility': 10,
'pressure': 1012,
'cloudcover': 88,
'heatindex': 8,
'dewpoint': 6,
'windchill': 6,
'windgust': 21,
'feelslike': 6,
'chanceofrain': 0,
'chanceofremdry': 89,
'chanceofwindy': 0,
'chanceofovercast': 88,
'chanceofsunshine': 15,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 1},
{'time': '600',
'temperature': 7,
'wind_speed': 17,
'wind_degree': 239,
'wind_dir': 'WSW',
'weather_code': 122,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Overcast'],
'precip': 0,
'humidity': 90,
'visibility': 10,
'pressure': 1012,
'cloudcover': 100,
'heatindex': 7,
'dewpoint': 6,
'windchill': 4,
'windgust': 24,
'feelslike': 4,
'chanceofrain': 0,
'chanceofremdry': 84,
'chanceofwindy': 0,
'chanceofovercast': 90,
'chanceofsunshine': 15,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 2},
{'time': '900',
'temperature': 8,
'wind_speed': 21,
'wind_degree': 248,
'wind_dir': 'WSW',
'weather_code': 296,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png'],
'weather_descriptions': ['Light rain'],
'precip': 0.5,
'humidity': 88,
'visibility': 10,
'pressure': 1012,
'cloudcover': 100,
'heatindex': 8,
'dewpoint': 6,
'windchill': 5,
'windgust': 27,
'feelslike': 5,
'chanceofrain': 28,
'chanceofremdry': 57,
'chanceofwindy': 0,
'chanceofovercast': 83,
'chanceofsunshine': 11,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 2},
{'time': '1200',
'temperature': 10,
'wind_speed': 24,
'wind_degree': 259,
'wind_dir': 'WSW',
'weather_code': 293,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png'],
'weather_descriptions': ['Patchy light rain'],
'precip': 1.3,
'humidity': 71,
'visibility': 9,
'pressure': 1012,
'cloudcover': 85,
'heatindex': 10,
'dewpoint': 4,
'windchill': 7,
'windgust': 28,
'feelslike': 7,
'chanceofrain': 82,
'chanceofremdry': 0,
'chanceofwindy': 0,
'chanceofovercast': 87,
'chanceofsunshine': 0,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 2},
{'time': '1500',
'temperature': 12,
'wind_speed': 23,
'wind_degree': 273,
'wind_dir': 'W',
'weather_code': 176,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png'],
'weather_descriptions': ['Patchy rain possible'],
'precip': 0.7,
'humidity': 49,
'visibility': 9,
'pressure': 1012,
'cloudcover': 66,
'heatindex': 12,
'dewpoint': 2,
'windchill': 10,
'windgust': 27,
'feelslike': 10,
'chanceofrain': 79,
'chanceofremdry': 0,
'chanceofwindy': 0,
'chanceofovercast': 89,
'chanceofsunshine': 0,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 3},
{'time': '1800',
'temperature': 11,
'wind_speed': 21,
'wind_degree': 277,
'wind_dir': 'W',
'weather_code': 116,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'],
'weather_descriptions': ['Partly cloudy'],
'precip': 0.1,
'humidity': 54,
'visibility': 9,
'pressure': 1011,
'cloudcover': 73,
'heatindex': 11,
'dewpoint': 2,
'windchill': 9,
'windgust': 26,
'feelslike': 9,
'chanceofrain': 56,
'chanceofremdry': 30,
'chanceofwindy': 0,
'chanceofovercast': 68,
'chanceofsunshine': 27,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 4},
{'time': '2100',
'temperature': 8,
'wind_speed': 17,
'wind_degree': 273,
'wind_dir': 'W',
'weather_code': 176,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png'],
'weather_descriptions': ['Patchy rain possible'],
'precip': 0,
'humidity': 75,
'visibility': 10,
'pressure': 1013,
'cloudcover': 61,
'heatindex': 8,
'dewpoint': 3,
'windchill': 5,
'windgust': 24,
'feelslike': 5,
'chanceofrain': 22,
'chanceofremdry': 60,
'chanceofwindy': 0,
'chanceofovercast': 59,
'chanceofsunshine': 53,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 1}]},
'2021-05-12': {'date': '2021-05-12',
'date_epoch': 1620777600,
'astro': {'sunrise': '05:27 AM',
'sunset': '08:15 PM',
'moonrise': '05:59 AM',
'moonset': '09:19 PM',
'moon_phase': 'Waxing Crescent',
'moon_illumination': 14},
'mintemp': 5,
'maxtemp': 15,
'avgtemp': 10,
'totalsnow': 0,
'sunhour': 11.4,
'uv_index': 4,
'hourly': [{'time': '0',
'temperature': 6,
'wind_speed': 16,
'wind_degree': 266,
'wind_dir': 'W',
'weather_code': 122,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Overcast'],
'precip': 0.1,
'humidity': 85,
'visibility': 10,
'pressure': 1013,
'cloudcover': 100,
'heatindex': 6,
'dewpoint': 4,
'windchill': 3,
'windgust': 21,
'feelslike': 3,
'chanceofrain': 44,
'chanceofremdry': 29,
'chanceofwindy': 0,
'chanceofovercast': 92,
'chanceofsunshine': 4,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 1},
{'time': '300',
'temperature': 6,
'wind_speed': 13,
'wind_degree': 270,
'wind_dir': 'W',
'weather_code': 122,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Overcast'],
'precip': 0,
'humidity': 85,
'visibility': 10,
'pressure': 1013,
'cloudcover': 98,
'heatindex': 6,
'dewpoint': 4,
'windchill': 3,
'windgust': 18,
'feelslike': 3,
'chanceofrain': 0,
'chanceofremdry': 88,
'chanceofwindy': 0,
'chanceofovercast': 90,
'chanceofsunshine': 13,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 1},
{'time': '600',
'temperature': 7,
'wind_speed': 11,
'wind_degree': 293,
'wind_dir': 'WNW',
'weather_code': 122,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Overcast'],
'precip': 0,
'humidity': 81,
'visibility': 10,
'pressure': 1016,
'cloudcover': 94,
'heatindex': 7,
'dewpoint': 3,
'windchill': 4,
'windgust': 16,
'feelslike': 4,
'chanceofrain': 0,
'chanceofremdry': 91,
'chanceofwindy': 0,
'chanceofovercast': 88,
'chanceofsunshine': 15,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 2},
{'time': '900',
'temperature': 10,
'wind_speed': 13,
'wind_degree': 324,
'wind_dir': 'NW',
'weather_code': 176,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png'],
'weather_descriptions': ['Patchy rain possible'],
'precip': 0,
'humidity': 60,
'visibility': 10,
'pressure': 1018,
'cloudcover': 98,
'heatindex': 10,
'dewpoint': 3,
'windchill': 8,
'windgust': 15,
'feelslike': 8,
'chanceofrain': 23,
'chanceofremdry': 61,
'chanceofwindy': 0,
'chanceofovercast': 93,
'chanceofsunshine': 7,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 3},
{'time': '1200',
'temperature': 14,
'wind_speed': 14,
'wind_degree': 344,
'wind_dir': 'NNW',
'weather_code': 176,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png'],
'weather_descriptions': ['Patchy rain possible'],
'precip': 0.1,
'humidity': 42,
'visibility': 10,
'pressure': 1018,
'cloudcover': 95,
'heatindex': 14,
'dewpoint': 1,
'windchill': 13,
'windgust': 16,
'feelslike': 13,
'chanceofrain': 66,
'chanceofremdry': 0,
'chanceofwindy': 0,
'chanceofovercast': 90,
'chanceofsunshine': 0,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 3},
{'time': '1500',
'temperature': 15,
'wind_speed': 10,
'wind_degree': 332,
'wind_dir': 'NNW',
'weather_code': 116,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'],
'weather_descriptions': ['Partly cloudy'],
'precip': 0.1,
'humidity': 39,
'visibility': 10,
'pressure': 1018,
'cloudcover': 72,
'heatindex': 15,
'dewpoint': 1,
'windchill': 14,
'windgust': 12,
'feelslike': 14,
'chanceofrain': 41,
'chanceofremdry': 28,
'chanceofwindy': 0,
'chanceofovercast': 72,
'chanceofsunshine': 23,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 4},
{'time': '1800',
'temperature': 14,
'wind_speed': 6,
'wind_degree': 271,
'wind_dir': 'W',
'weather_code': 116,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'],
'weather_descriptions': ['Partly cloudy'],
'precip': 0,
'humidity': 44,
'visibility': 10,
'pressure': 1019,
'cloudcover': 44,
'heatindex': 14,
'dewpoint': 2,
'windchill': 14,
'windgust': 9,
'feelslike': 14,
'chanceofrain': 0,
'chanceofremdry': 85,
'chanceofwindy': 0,
'chanceofovercast': 37,
'chanceofsunshine': 75,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 4},
{'time': '2100',
'temperature': 12,
'wind_speed': 6,
'wind_degree': 239,
'wind_dir': 'WSW',
'weather_code': 116,
'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'],
'weather_descriptions': ['Partly cloudy'],
'precip': 0,
'humidity': 56,
'visibility': 10,
'pressure': 1020,
'cloudcover': 41,
'heatindex': 12,
'dewpoint': 3,
'windchill': 12,
'windgust': 10,
'feelslike': 12,
'chanceofrain': 0,
'chanceofremdry': 84,
'chanceofwindy': 0,
'chanceofovercast': 37,
'chanceofsunshine': 82,
'chanceoffrost': 0,
'chanceofhightemp': 0,
'chanceoffog': 0,
'chanceofsnow': 0,
'chanceofthunder': 0,
'uv_index': 1}]}}}
How do I iterate through this output and create a new data frame to look like this:
(And get only the date time hour, and rain values)
Date
precip at time 0
precip at time 300
...
2021-05-11
0.1
0
...
2021-05-12
0.1
0
...
...
...
...
...
Also, I want to convert the date column to Month Day ,Year. ie. 2021-05-11 -> May 05, 2021. I tried using
df['Date'] = datetime.datetime.strftime('%b %d,%Y')
then
df['Date'] = pandas.to_datetime(df['Date'])
But I get this error:
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
Create dataframe
This will create a dataframe with the required data and column names.
import pandas as pd
### code to get data
forecast={}
for date in api_response['forecast']:
precip = {f"precip at time {api_response['forecast'][date]['hourly'][hour]['time']}":
api_response['forecast'][date]['hourly'][hour]['precip'] for hour in range(0, 8)}
forecast[date] = precip
df = pd.DataFrame.from_dict(forecast, orient='index', dtype=float).reset_index()
df.rename(columns={'index':'Date'}, inplace=True)
print(df)
Create dataframe and format headers
If you wanted the times in HH:MM format make these changes/additions to the code.
import json
import pandas as pd
### code to get data
forecast={}
for date in api_response['forecast']:
precip = {api_response['forecast'][date]['hourly'][hour]['time']:
api_response['forecast'][date]['hourly'][hour]['precip'] for hour in range(0, 8)}
forecast[date] = precip
df = pd.DataFrame.from_dict(forecast, orient='index', dtype=float).reset_index()
cols = ['Date' if col=='index' else f'precip at time {col.zfill(4)[:2]+":"+col.zfill(4)[2:]}' for col in df.columns]
df.columns = cols
print(df)
Change Date column format
To change the format of the Date column we first need to convert it to a datetime, which we can do using pd.to_datetime.
We can then chain strftime to get the required format, May 05, 2021.
For a full list of the directives you can use with strftime see here.
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%B %d, %Y')

Python - comparing two dicts values for contains and showing matches

I can see lots of similar questions but imo not having much luck finding an answer.
I have two dictionaries with values I want to match against but with different keys. ive attempted a match query but its returning empty. I think its because of the miss matching key names maybe? or not iterating the k,v pairs? But im not sure what to do here
interface_list = [
{'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/0 *** Uplink ***', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/1', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 19, 'Caption': 'GigabitEthernet0/0/2', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 19, 'Caption': 'Tunnel100', 'ifType': 131, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 20, 'Caption': 'Vlan5', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 21, 'Caption': 'Vlan10', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
{'ifIndex': 22, 'Caption': 'Vlan15', 'ifType': 53, 'ifSubType': 0, 'InterfaceID': 0, 'Manageable': True, 'ifSpeed': 0.0, 'ifAdminStatus': 0, 'ifOperStatus': 4},
]
wanted_interfaces = [{'resource': 'GigabitEthernet0/0/0'}, {'resource': 'Vlan5'}]
>>> matches = [i for str(i) in wanted_interfaces if i in interface_list]
>>> matches
[]
it should hopefully return the record containing 'GigabitEthernet0/0/0 * Uplink *' as a match
For a comprehensive scan (assuming you want to check every value in every dict in both lists), you would have to do something like:
matches = [
v for d1 in interface_list for v in d1.values()
if any(isinstance(v, str) and vw in v for d2 in wanted_interfaces for vw in d2.values())
]
# ['GigabitEthernet0/0/0 *** Uplink ***']

How to get a mapping of country codes to international number prefixes in Python? [duplicate]

This question already has answers here:
Get country name from Country code in python?
(3 answers)
Closed 4 years ago.
I'm interested in getting a mapping of country codes to international phone number prefixes, like so:
{'US': '+1', 'GB': '+44', 'DE': '+49', ...}
One library that probably contains this information is python-phonenumbers. However, after a quick perusal of the source code I wasn't able to find where this information is stored. For example, the shortdata/region_DE.py module looks like this:
"""Auto-generated file, do not edit by hand. DE metadata"""
from ..phonemetadata import NumberFormat, PhoneNumberDesc, PhoneMetadata
PHONE_METADATA_DE = PhoneMetadata(id='DE', country_code=None, international_prefix=None,
general_desc=PhoneNumberDesc(national_number_pattern='1\\d{2,5}', possible_length=(3, 6)),
toll_free=PhoneNumberDesc(national_number_pattern='116\\d{3}', example_number='116000', possible_length=(6,)),
emergency=PhoneNumberDesc(national_number_pattern='11[02]', example_number='112', possible_length=(3,)),
short_code=PhoneNumberDesc(national_number_pattern='11(?:[025]|6(?:00[06]|1(?:1[17]|23)))', example_number='115', possible_length=(3, 6)),
short_data=True)
It seems like the country_code and international_prefix fields are None. How can I get such a mapping (possibly with a different library)?
You can get the mapping you want using pycountry and phonenumbers, along with a simple dictionary comprehension:
import phonenumbers as pn
import pycountry
dct = {c.alpha_2: pn.country_code_for_region(c.alpha_2) for c in pycountry.countries}
print(dct)
Output:
{'SK': 421, 'KI': 686, 'LV': 371, 'GH': 233, 'JP': 81, 'SA': 966, 'TD': 235, 'SX': 1, 'CY': 357, 'CH': 41, 'EG': 20, 'PA': 507, 'KP': 850, 'CO': 57, 'GW': 245, 'KG': 996, 'AW': 297, 'FM': 691, 'SB': 677, 'HR': 385, 'PY': 595, 'BG': 359, 'IQ': 964, 'ID': 62, 'GQ': 240, 'CA': 1, 'CG': 242, 'MO': 853, 'SL': 232, 'LA': 856, 'OM': 968, 'MP': 1, 'DK': 45, 'FI': 358, 'DO': 1, 'BM': 1, 'GN': 224, 'NE': 227, 'ER': 291, 'DE': 49, 'UM': 0, 'CM': 237, 'PR': 1, 'RO': 40, 'AZ': 994, 'DZ': 213, 'BW': 267, 'MK': 389, 'HN': 504, 'IS': 354, 'SJ': 47, 'ME': 382, 'NR': 674, 'AD': 376, 'BY': 375, 'RE': 262, 'PG': 675, 'SO': 252, 'NO': 47, 'CC': 61, 'EE': 372, 'BN': 673, 'AU': 61, 'HM': 0, 'ML': 223, 'BD': 880, 'GE': 995, 'US': 1, 'UY': 598, 'SM': 378, 'NG': 234, 'BE': 32, 'KY': 1, 'AR': 54, 'CR': 506, 'VA': 39, 'YE': 967, 'TR': 90, 'CV': 238, 'DM': 1, 'ZM': 260, 'BR': 55, 'MG': 261, 'BL': 590, 'FJ': 679, 'SH': 290, 'KN': 1, 'ZA': 27, 'CF': 236, 'ZW': 263, 'PL': 48, 'SV': 503, 'QA': 974, 'MN': 976, 'SE': 46, 'JE': 44, 'PS': 970, 'MZ': 258, 'TK': 690, 'PM': 508, 'CW': 599, 'HK': 852, 'LB': 961, 'SY': 963, 'LC': 1, 'IE': 353, 'RW': 250, 'NL': 31, 'MA': 212, 'GM': 220, 'IR': 98, 'AT': 43, 'SZ': 268, 'GT': 502, 'MT': 356, 'BQ': 599, 'MX': 52, 'NC': 687, 'CK': 682, 'SI': 386, 'VE': 58, 'IM': 44, 'AM': 374, 'SD': 249, 'LY': 218, 'LI': 423, 'TN': 216, 'UG': 256, 'RU': 7, 'DJ': 253, 'IL': 972, 'TM': 993, 'BF': 226, 'GF': 594, 'TO': 676, 'GI': 350, 'MH': 692, 'UZ': 998, 'PF': 689, 'KZ': 7, 'GA': 241, 'PE': 51, 'TV': 688, 'BT': 975, 'MQ': 596, 'MF': 590, 'AF': 93, 'IN': 91, 'AX': 358, 'BH': 973, 'JM': 1, 'MY': 60, 'BO': 591, 'AI': 1, 'SR': 597, 'ET': 251, 'ES': 34, 'TF': 0, 'GU': 1, 'BJ': 229, 'SS': 211, 'KE': 254, 'BZ': 501, 'IO': 246, 'MU': 230, 'CL': 56, 'MD': 373, 'LU': 352, 'TJ': 992, 'EC': 593, 'VG': 1, 'NZ': 64, 'VU': 678, 'FO': 298, 'LR': 231, 'AL': 355, 'GB': 44, 'AS': 1, 'IT': 39, 'TC': 1, 'TW': 886, 'BI': 257, 'HU': 36, 'TL': 670, 'GG': 44, 'PN': 0, 'SG': 65, 'LS': 266, 'KH': 855, 'FR': 33, 'BV': 0, 'CX': 61, 'AE': 971, 'LT': 370, 'PT': 351, 'KR': 82, 'BB': 1, 'TG': 228, 'AQ': 0, 'EH': 212, 'AG': 1, 'VN': 84, 'CI': 225, 'BS': 1, 'GL': 299, 'MW': 265, 'NU': 683, 'NF': 672, 'LK': 94, 'MS': 1, 'GP': 590, 'NP': 977, 'PW': 680, 'PK': 92, 'WF': 681, 'BA': 387, 'KM': 269, 'JO': 962, 'CU': 53, 'GR': 30, 'YT': 262, 'RS': 381, 'NA': 264, 'ST': 239, 'SC': 248, 'CN': 86, 'CD': 243, 'GS': 0, 'KW': 965, 'MM': 95, 'AO': 244, 'MV': 960, 'UA': 380, 'TT': 1, 'FK': 500, 'WS': 685, 'CZ': 420, 'PH': 63, 'VI': 1, 'TZ': 255, 'MR': 222, 'MC': 377, 'SN': 221, 'HT': 509, 'VC': 1, 'NI': 505, 'GD': 1, 'GY': 592, 'TH': 66}
I have just found a python library that must be perfect for your problem.
It's called PhoneISO3166.
This is the github link: GitHub phoneiso3166

Categories

Resources