How to change numbers in jsonl file and save it - python

I have a jsonl file with content
How to read a file and change the number after the label sign to a random number 0 or 1 and save the converted file back in python
{"idx": 0, "passage": {"questions": [{"idx": 0, "answers": [{"idx": 0, "label": 0}, {"idx": 1, "label": 0}, {"idx": 2, "label": 0}, {"idx": 3, "label": 0}]}, {"idx": 1, "answers": [{"idx": 4, "label": 0}, {"idx": 5, "label": 0}, {"idx": 6, "label": 0}, {"idx": 7, "label": 0}]}, {"idx": 2, "answers": [{"idx": 8, "label": 1}, {"idx": 9, "label": 0}, {"idx": 10, "label": 0}, {"idx": 11, "label": 0}]}, {"idx": 3, "answers": [{"idx": 12, "label": 0}, {"idx": 13, "label": 0}, {"idx": 14, "label": 0}, {"idx": 15, "label": 0}]}, {"idx": 4, "answers": [{"idx": 16, "label": 0}, {"idx": 17, "label": 0}, {"idx": 18, "label": 0}, {"idx": 19, "label": 0}, {"idx": 20, "label": 0}]}, {"idx": 5, "answers": [{"idx": 21, "label": 0}, {"idx": 22, "label": 0}, {"idx": 23, "label": 0}, {"idx": 24, "label": 0}, {"idx": 25, "label": 0}]}, {"idx": 6, "answers": [{"idx": 26, "label": 0}, {"idx": 27, "label": 0}, {"idx": 28, "label": 0}, {"idx": 29, "label": 0}, {"idx": 30, "label": 0}]}, {"idx": 7, "answers": [{"idx": 31, "label": 0}, {"idx": 32, "label": 0}, {"idx": 33, "label": 0}, {"idx": 34, "label": 0}, {"idx": 35, "label": 0}]}, {"idx": 8, "answers": [{"idx": 36, "label": 0}, {"idx": 37, "label": 0}, {"idx": 38, "label": 0}, {"idx": 39, "label": 0}, {"idx": 40, "label": 0}]}, {"idx": 9, "answers": [{"idx": 41, "label": 0}, {"idx": 42, "label": 0}, {"idx": 43, "label": 0}, {"idx": 44, "label": 0}, {"idx": 45, "label": 0}]}]}}
{"idx": 1, "passage": {"questions": [{"idx": 10, "answers": [{"idx": 46, "label": 0}, {"idx": 47, "label": 0}, {"idx": 48, "label": 0}, {"idx": 49, "label": 0}, {"idx": 50, "label": 0}]}, {"idx": 11, "answers": [{"idx": 51, "label": 0}, {"idx": 52, "label": 0}, {"idx": 53, "label": 0}, {"idx": 54, "label": 0}, {"idx": 55, "label": 0}]}]}}
{"idx": 2, "passage": {"questions": [{"idx": 12, "answers": [{"idx": 56, "label": 0}, {"idx": 57, "label": 0}, {"idx": 58, "label": 0}, {"idx": 59, "label": 0}, {"idx": 60, "label": 0}]}, {"idx": 13, "answers": [{"idx": 61, "label": 0}, {"idx": 62, "label": 0}, {"idx": 63, "label": 0}, {"idx": 64, "label": 0}, {"idx": 65, "label": 0}]}, {"idx": 14, "answers": [{"idx": 66, "label": 0}, {"idx": 67, "label": 0}, {"idx": 68, "label": 0}, {"idx": 69, "label": 0}, {"idx": 70, "label": 0}]}, {"idx": 15, "answers": [{"idx": 71, "label": 0}, {"idx": 72, "label": 0}, {"idx": 73, "label": 0}, {"idx": 74, "label": 0}, {"idx": 75, "label": 0}]}, {"idx": 16, "answers": [{"idx": 76, "label": 0}, {"idx": 77, "label": 0}, {"idx": 78, "label": 0}, {"idx": 79, "label": 0}, {"idx": 80, "label": 0}]}, {"idx": 17, "answers": [{"idx": 81, "label": 0}, {"idx": 82, "label": 0}, {"idx": 83, "label": 0}, {"idx": 84, "label": 0}, {"idx": 85, "label": 0}]}, {"idx": 18, "answers": [{"idx": 86, "label": 0}, {"idx": 87, "label": 0}, {"idx": 88, "label": 0}, {"idx": 89, "label": 0}, {"idx": 90, "label": 0}]}, {"idx": 19, "answers": [{"idx": 91, "label": 0}, {"idx": 92, "label": 0}, {"idx": 93, "label": 0}, {"idx": 94, "label": 0}, {"idx": 95, "label": 0}]}]}}
{"idx": 3, "passage": {"questions": [{"idx": 20, "answers": [{"idx": 96, "label": 0}, {"idx": 97, "label": 0}, {"idx": 98, "label": 0}, {"idx": 99, "label": 0}]}, {"idx": 21, "answers": [{"idx": 100, "label": 0}, {"idx": 101, "label": 0}, {"idx": 102, "label": 0}, {"idx": 103, "label": 0}]}, {"idx": 22, "answers": [{"idx": 104, "label": 0}, {"idx": 105, "label": 0}, {"idx": 106, "label": 0}, {"idx": 107, "label": 0}]}, {"idx": 23, "answers": [{"idx": 108, "label": 0}, {"idx": 109, "label": 0}, {"idx": 110, "label": 0}, {"idx": 111, "label": 0}]}, {"idx": 24, "answers": [{"idx": 112, "label": 0}, {"idx": 113, "label": 0}, {"idx": 114, "label": 0}, {"idx": 115, "label": 0}]}, {"idx": 25, "answers": [{"idx": 116, "label": 0}, {"idx": 117, "label": 0}, {"idx": 118, "label": 0}, {"idx": 119, "label": 0}]}, {"idx": 26, "answers": [{"idx": 120, "label": 0}, {"idx": 121, "label": 0}, {"idx": 122, "label": 0}, {"idx": 123, "label": 0}]}, {"idx": 27, "answers": [{"idx": 124, "label": 0}, {"idx": 125, "label": 0}, {"idx": 126, "label": 0}, {"idx": 127, "label": 0}]}]}}
{"idx": 4, "passage": {"questions": [{"idx": 28, "answers": [{"idx": 128, "label": 1}, {"idx": 129, "label": 1}, {"idx": 130, "label": 1}, {"idx": 131, "label": 1}, {"idx": 132, "label": 1}]}, {"idx": 29, "answers": [{"idx": 133, "label": 0}, {"idx": 134, "label": 1}, {"idx": 135, "label": 1}, {"idx": 136, "label": 0}, {"idx": 137, "label": 1}]}, {"idx": 30, "answers": [{"idx": 138, "label": 0}, {"idx": 139, "label": 0}, {"idx": 140, "label": 1}, {"idx": 141, "label": 0}, {"idx": 142, "label": 0}]}, {"idx": 31, "answers": [{"idx": 143, "label": 0}, {"idx": 144, "label": 0}, {"idx": 145, "label": 0}, {"idx": 146, "label": 0}, {"idx": 147, "label": 0}]}, {"idx": 32, "answers": [{"idx": 148, "label": 0}, {"idx": 149, "label": 0}, {"idx": 150, "label": 0}, {"idx": 151, "label": 0}, {"idx": 152, "label": 0}]}, {"idx": 33, "answers": [{"idx": 153, "label": 0}, {"idx": 154, "label": 1}, {"idx": 155, "label": 1}, {"idx": 156, "label": 1}, {"idx": 157, "label": 1}]}, {"idx": 34, "answers": [{"idx": 158, "label": 0}, {"idx": 159, "label": 0}, {"idx": 160, "label": 0}, {"idx": 161, "label": 0}, {"idx": 162, "label": 0}]}, {"idx": 35, "answers": [{"idx": 163, "label": 0}, {"idx": 164, "label": 1}, {"idx": 165, "label": 1}, {"idx": 166, "label": 0}, {"idx": 167, "label": 0}]}, {"idx": 36, "answers": [{"idx": 168, "label": 0}, {"idx": 169, "label": 0}, {"idx": 170, "label": 1}, {"idx": 171, "label": 0}, {"idx": 172, "label": 0}]}, {"idx": 37, "answers": [{"idx": 173, "label": 1}, {"idx": 174, "label": 0}, {"idx": 175, "label": 0}, {"idx": 176, "label": 0}, {"idx": 177, "label": 0}]}, {"idx": 38, "answers": [{"idx": 178, "label": 0}, {"idx": 179, "label": 1}, {"idx": 180, "label": 1}, {"idx": 181, "label": 0}, {"idx": 182, "label": 1}]}, {"idx": 39, "answers": [{"idx": 183, "label": 1}, {"idx": 184, "label": 1}, {"idx": 185, "label": 1}, {"idx": 186, "label": 0}, {"idx": 187, "label": 0}]}, {"idx": 40, "answers": [{"idx": 188, "label": 0}, {"idx": 189, "label": 0}, {"idx": 190, "label": 1}, {"idx": 191, "label": 0}, {"idx": 192, "label": 0}]}, {"idx": 41, "answers": [{"idx": 193, "label": 0}, {"idx": 194, "label": 0}, {"idx": 195, "label": 0}, {"idx": 196, "label": 0}, {"idx": 197, "label": 0}]}]}}
{"idx": 5, "passage": {"questions": [{"idx": 42, "answers": [{"idx": 198, "label": 0}, {"idx": 199, "label": 0}]}, {"idx": 43, "answers": [{"idx": 200, "label": 1}, {"idx": 201, "label": 0}]}, {"idx": 44, "answers": [{"idx": 202, "label": 0}, {"idx": 203, "label": 0}, {"idx": 204, "label": 0}, {"idx": 205, "label": 0}]}, {"idx": 45, "answers": [{"idx": 206, "label": 0}, {"idx": 207, "label": 0}, {"idx": 208, "label": 0}, {"idx": 209, "label": 0}]}, {"idx": 46, "answers": [{"idx": 210, "label": 0}, {"idx": 211, "label": 0}, {"idx": 212, "label": 0}, {"idx": 213, "label": 0}]}, {"idx": 47, "answers": [{"idx": 214, "label": 0}, {"idx": 215, "label": 0}, {"idx": 216, "label": 0}, {"idx": 217, "label": 0}]}, {"idx": 48, "answers": [{"idx": 218, "label": 0}, {"idx": 219, "label": 0}, {"idx": 220, "label": 0}, {"idx": 221, "label": 0}, {"idx": 222, "label": 0}]}, {"idx": 49, "answers": [{"idx": 223, "label": 1}, {"idx": 224, "label": 0}, {"idx": 225, "label": 0}, {"idx": 226, "label": 0}, {"idx": 227, "label": 0}]}, {"idx": 50, "answers": [{"idx": 228, "label": 1}, {"idx": 229, "label": 0}, {"idx": 230, "label": 0}, {"idx": 231, "label": 0}, {"idx": 232, "label": 0}]}, {"idx": 51, "answers": [{"idx": 233, "label": 0}, {"idx": 234, "label": 0}, {"idx": 235, "label": 0}, {"idx": 236, "label": 0}, {"idx": 237, "label": 0}]}, {"idx": 52, "answers": [{"idx": 238, "label": 1}, {"idx": 239, "label": 0}, {"idx": 240, "label": 0}, {"idx": 241, "label": 0}, {"idx": 242, "label": 0}]}, {"idx": 53, "answers": [{"idx": 243, "label": 0}, {"idx": 244, "label": 0}, {"idx": 245, "label": 0}, {"idx": 246, "label": 1}, {"idx": 247, "label": 1}]}, {"idx": 54, "answers": [{"idx": 248, "label": 1}, {"idx": 249, "label": 1}, {"idx": 250, "label": 1}, {"idx": 251, "label": 1}, {"idx": 252, "label": 0}]}, {"idx": 55, "answers": [{"idx": 253, "label": 0}, {"idx": 254, "label": 0}, {"idx": 255, "label": 0}, {"idx": 256, "label": 0}, {"idx": 257, "label": 0}]}]}}
{"idx": 6, "passage": {"questions": [{"idx": 56, "answers": [{"idx": 258, "label": 1}, {"idx": 259, "label": 0}, {"idx": 260, "label": 1}, {"idx": 261, "label": 0}]}, {"idx": 57, "answers": [{"idx": 262, "label": 1}, {"idx": 263, "label": 1}, {"idx": 264, "label": 1}]}, {"idx": 58, "answers": [{"idx": 265, "label": 1}, {"idx": 266, "label": 1}, {"idx": 267, "label": 1}, {"idx": 268, "label": 1}]}, {"idx": 59, "answers": [{"idx": 269, "label": 1}, {"idx": 270, "label": 1}, {"idx": 271, "label": 1}, {"idx": 272, "label": 1}]}, {"idx": 60, "answers": [{"idx": 273, "label": 1}, {"idx": 274, "label": 1}, {"idx": 275, "label": 1}, {"idx": 276, "label": 1}]}, {"idx": 61, "answers": [{"idx": 277, "label": 1}, {"idx": 278, "label": 1}, {"idx": 279, "label": 1}, {"idx": 280, "label": 1}]}]}}
{"idx": 7, "passage": {"questions": [{"idx": 62, "answers": [{"idx": 281, "label": 0}, {"idx": 282, "label": 1}, {"idx": 283, "label": 1}, {"idx": 284, "label": 1}, {"idx": 285, "label": 0}]}, {"idx": 63, "answers": [{"idx": 286, "label": 0}, {"idx": 287, "label": 0}, {"idx": 288, "label": 0}, {"idx": 289, "label": 0}, {"idx": 290, "label": 1}]}, {"idx": 64, "answers": [{"idx": 291, "label": 0}, {"idx": 292, "label": 0}, {"idx": 293, "label": 0}, {"idx": 294, "label": 0}, {"idx": 295, "label": 0}]}, {"idx": 65, "answers": [{"idx": 296, "label": 1}, {"idx": 297, "label": 1}, {"idx": 298, "label": 1}, {"idx": 299, "label": 1}, {"idx": 300, "label": 1}]}, {"idx": 66, "answers": [{"idx": 301, "label": 1}, {"idx": 302, "label": 0}, {"idx": 303, "label": 1}, {"idx": 304, "label": 0}, {"idx": 305, "label": 1}]}, {"idx": 67, "answers": [{"idx": 306, "label": 0}, {"idx": 307, "label": 0}, {"idx": 308, "label": 0}, {"idx": 309, "label": 1}, {"idx": 310, "label": 1}]}, {"idx": 68, "answers": [{"idx": 311, "label": 0}, {"idx": 312, "label": 0}, {"idx": 313, "label": 0}, {"idx": 314, "label": 1}, {"idx": 315, "label": 0}]}, {"idx": 69, "answers": [{"idx": 316, "label": 1}, {"idx": 317, "label": 1}, {"idx": 318, "label": 1}, {"idx": 319, "label": 1}, {"idx": 320, "label": 1}]}]}}
{"idx": 8, "passage": {"questions": [{"idx": 70, "answers": [{"idx": 321, "label": 0}, {"idx": 322, "label": 0}, {"idx": 323, "label": 0}, {"idx": 324, "label": 0}]}, {"idx": 71, "answers": [{"idx": 325, "label": 1}, {"idx": 326, "label": 0}, {"idx": 327, "label": 0}, {"idx": 328, "label": 0}]}, {"idx": 72, "answers": [{"idx": 329, "label": 0}, {"idx": 330, "label": 0}, {"idx": 331, "label": 0}, {"idx": 332, "label": 0}, {"idx": 333, "label": 0}]}, {"idx": 73, "answers": [{"idx": 334, "label": 0}, {"idx": 335, "label": 0}, {"idx": 336, "label": 0}, {"idx": 337, "label": 0}, {"idx": 338, "label": 0}]}, {"idx": 74, "answers": [{"idx": 339, "label": 0}, {"idx": 340, "label": 0}, {"idx": 341, "label": 0}, {"idx": 342, "label": 1}, {"idx": 343, "label": 1}]}, {"idx": 75, "answers": [{"idx": 344, "label": 1}, {"idx": 345, "label": 1}, {"idx": 346, "label": 0}, {"idx": 347, "label": 0}, {"idx": 348, "label": 0}]}, {"idx": 76, "answers": [{"idx": 349, "label": 0}, {"idx": 350, "label": 1}, {"idx": 351, "label": 0}, {"idx": 352, "label": 0}, {"idx": 353, "label": 0}]}, {"idx": 77, "answers": [{"idx": 354, "label": 0}, {"idx": 355, "label": 0}, {"idx": 356, "label": 0}, {"idx": 357, "label": 1}, {"idx": 358, "label": 0}]}, {"idx": 78, "answers": [{"idx": 359, "label": 0}, {"idx": 360, "label": 1}, {"idx": 361, "label": 0}, {"idx": 362, "label": 0}, {"idx": 363, "label": 0}]}, {"idx": 79, "answers": [{"idx": 364, "label": 0}, {"idx": 365, "label": 0}, {"idx": 366, "label": 0}, {"idx": 367, "label": 0}, {"idx": 368, "label": 0}]}, {"idx": 80, "answers": [{"idx": 369, "label": 0}, {"idx": 370, "label": 0}, {"idx": 371, "label": 0}, {"idx": 372, "label": 0}, {"idx": 373, "label": 0}]}, {"idx": 81, "answers": [{"idx": 374, "label": 0}, {"idx": 375, "label": 0}, {"idx": 376, "label": 0}, {"idx": 377, "label": 0}, {"idx": 378, "label": 0}]}]}}
{"idx": 9, "passage": {"questions": [{"idx": 82, "answers": [{"idx": 379, "label": 0}, {"idx": 380, "label": 0}, {"idx": 381, "label": 0}, {"idx": 382, "label": 0}]}, {"idx": 83, "answers": [{"idx": 383, "label": 0}, {"idx": 384, "label": 1}, {"idx": 385, "label": 0}, {"idx": 386, "label": 0}]}, {"idx": 84, "answers": [{"idx": 387, "label": 0}, {"idx": 388, "label": 0}, {"idx": 389, "label": 0}, {"idx": 390, "label": 0}]}, {"idx": 85, "answers": [{"idx": 391, "label": 1}, {"idx": 392, "label": 0}, {"idx": 393, "label": 1}, {"idx": 394, "label": 1}]}, {"idx": 86, "answers": [{"idx": 395, "label": 0}, {"idx": 396, "label": 0}, {"idx": 397, "label": 0}, {"idx": 398, "label": 0}]}, {"idx": 87, "answers": [{"idx": 399, "label": 0}, {"idx": 400, "label": 0}, {"idx": 401, "label": 0}, {"idx": 402, "label": 1}]}, {"idx": 88, "answers": [{"idx": 403, "label": 0}, {"idx": 404, "label": 0}, {"idx": 405, "label": 1}, {"idx": 406, "label": 0}]}, {"idx": 89, "answers": [{"idx": 407, "label": 0}, {"idx": 408, "label": 0}, {"idx": 409, "label": 0}, {"idx": 410, "label": 1}]}]}}

import json
import random
# read each decoded JSON line into a list
with open('test.jsonl',encoding='utf8') as f:
data = [json.loads(line) for line in f]
# walk the structure and change the labels
for item in data:
for q in item['passage']['questions']:
for a in q['answers']:
a['label'] = random.randint(0,1)
# write each JSON line back to a new file
with open('test2.jsonl','w',encoding='utf8') as f:
for item in data:
json.dump(item,f)
print(file=f) # add a newline
You could write back to the same file, but safer to delete and rename once written successfully.

Hope this different and Fast approach will work
import json
import numpy as np
import random
def find(original, sub):
return [i+9 for i in range(len(original)) if original.startswith(sub, i)]
def split_into_parts(number, n=2):
if number % n==0:
return [int(number/2),int(number/2)]
else:
return [int(number/2),int(number/2)+1]
def get_ranodom_list(num):
o,z=split_into_parts(num,2)
ls=[0]*o+[1]*z
random.shuffle(ls)
return ls
d={"idx": 0, "passage": {"questions": [{"idx": 0, "answers": [{"idx": 0, "label": 0}, {"idx": 1, "label": 0}, {"idx": 2, "label": 0}, {"idx": 3, "label": 0}]}, {"idx": 1, "answers": [{"idx": 4, "label": 0}, {"idx": 5, "label": 0}, {"idx": 6, "label": 0}, {"idx": 7, "label": 0}]}, {"idx": 2, "answers": [{"idx": 8, "label": 1}, {"idx": 9, "label": 0}, {"idx": 10, "label": 0}, {"idx": 11, "label": 0}]}, {"idx": 3, "answers": [{"idx": 12, "label": 0}, {"idx": 13, "label": 0}, {"idx": 14, "label": 0}, {"idx": 15, "label": 0}]}, {"idx": 4, "answers": [{"idx": 16, "label": 0}, {"idx": 17, "label": 0}, {"idx": 18, "label": 0}, {"idx": 19, "label": 0}, {"idx": 20, "label": 0}]}, {"idx": 5, "answers": [{"idx": 21, "label": 0}, {"idx": 22, "label": 0}, {"idx": 23, "label": 0}, {"idx": 24, "label": 0}, {"idx": 25, "label": 0}]}, {"idx": 6, "answers": [{"idx": 26, "label": 0}, {"idx": 27, "label": 0}, {"idx": 28, "label": 0}, {"idx": 29, "label": 0}, {"idx": 30, "label": 0}]}, {"idx": 7, "answers": [{"idx": 31, "label": 0}, {"idx": 32, "label": 0}, {"idx": 33, "label": 0}, {"idx": 34, "label": 0}, {"idx": 35, "label": 0}]}, {"idx": 8, "answers": [{"idx": 36, "label": 0}, {"idx": 37, "label": 0}, {"idx": 38, "label": 0}, {"idx": 39, "label": 0}, {"idx": 40, "label": 0}]}, {"idx": 9, "answers": [{"idx": 41, "label": 0}, {"idx": 42, "label": 0}, {"idx": 43, "label": 0}, {"idx": 44, "label": 0}, {"idx": 45, "label": 0}]}]}}
original = json.dumps(d)
result=find(original,'"label": ')
num=len(result)
zo_list=get_ranodom_list(num)
temp = list(original)
counter=0
for i in range(len(original)):
if i in result:
temp[i] = str(zo_list[counter])
counter+=1
res = ''.join(temp)
print(res)

Related

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

Tabulate incoming table information in json format as dataframe

Output from my table.py file is as follows. My goal is to create a table in dataframe format using the information from here. (I caught the values of the tables in different formats in the pdf file). values contain information corresponding to rows or columns. The other alignment information is the information returned from the csv about their location in the pdf file.now i need to tabulate them in dataframe but i couldn't find an easy way. I need your help.
{"returnData": [{"value": "Art.", "left": 123, "top": 445, "width": 16, "height": 14, "pageNo": 1,"prob": 0.8870015740394592},{"value": "Nr.", "left": 143, "top": 445, "width": 12, "height": 14, "pageNo": 1, "prob": 0.8796614408493042}, {"value": "Beschreibung", "left": 197, "top": 446, "width": 72, "height": 14, "pageNo": 1, "prob": 0.9776557683944702}, {"value": "Kolli", "left": 501, "top": 448, "width": 25, "height": 14, "pageNo": 1, "prob": 0.8893064260482788}, {"value": "Inhalt", "left": 531, "top": 448, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9761049747467041}, {"value": "Menge", "left": 593, "top": 449, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9946377873420715},{"value": "Preis", "left": 665, "top": 449, "width": 20, "height": 14, "pageNo": 1, "prob": 0.974210262298584},{"value": "€", "left": 689, "top": 449, "width": 4, "height": 14, "pageNo": 1, "prob": 0.9806452393531799},
{"value": "Betrag", "left": 743, "top": 450, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9663920998573303},{"value": "€", "left": 778, "top": 450, "width": 5, "height": 14, "pageNo": 1, "prob": 0.9893004298210144},...]}
This should be easy enough with json_normalize.
When json_string is the string you mentioned, try:
import pandas as pd
json_string = {
"returnData":[
{"value": "Art.", "left": 123, "top": 445, "width": 16, "height": 14, "pageNo": 1,"prob": 0.8870015740394592},
{"value": "Nr.", "left": 143, "top": 445, "width": 12, "height": 14, "pageNo": 1, "prob": 0.8796614408493042},
{"value": "Beschreibung", "left": 197, "top": 446, "width": 72, "height": 14, "pageNo": 1, "prob": 0.9776557683944702},
{"value": "Kolli", "left": 501, "top": 448, "width": 25, "height": 14, "pageNo": 1, "prob": 0.8893064260482788},
{"value": "Inhalt", "left": 531, "top": 448, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9761049747467041},
{"value": "Menge", "left": 593, "top": 449, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9946377873420715},
{"value": "Preis", "left": 665, "top": 449, "width": 20, "height": 14, "pageNo": 1, "prob": 0.974210262298584},
{"value": "€", "left": 689, "top": 449, "width": 4, "height": 14, "pageNo": 1, "prob": 0.9806452393531799},
{"value": "Betrag", "left": 743, "top": 450, "width": 30, "height": 14, "pageNo": 1, "prob": 0.9663920998573303},
{"value": "€", "left": 778, "top": 450, "width": 5, "height": 14, "pageNo": 1, "prob": 0.9893004298210144}
]
}
pd.json_normalize(json_string['returnData'])
returns:
value left top width height pageNo prob
Art. 123 445 16 14 1 0.887002
Nr. 143 445 12 14 1 0.879661
Beschreibung 197 446 72 14 1 0.977656
Kolli 501 448 25 14 1 0.889306
Inhalt 531 448 30 14 1 0.976105
Menge 593 449 30 14 1 0.994638
Preis 665 449 20 14 1 0.974210
€ 689 449 4 14 1 0.980645
Betrag 743 450 30 14 1 0.966392
€ 778 450 5 14 1 0.989300
Since these are column values, you can determine the left-rightbound of a column. f.e. all Art. Nr. values will be between ±123 and ±197, Beschreibung between ±197 and ±501. So you can map the following data according to that logic.

COCO .json file contains strange segmentation values in the annotattions, how to convert these?

I have a COCO format .json file which contains strange values in the annotation section. Most segmentations here are fine, but some contain size and counts in non human-readable format.
When training my model, I run into errors because of the weird segmentation values. I have read somewhere these are in RLE format but I am not sure. I should be able to use bitmask instead of polygon to train my model, but I prefer to handle the root cause and change these segmentations to the normal format. What is their type, can they be converted to normal segmentation format, and if so, how can I do that?
{'id': 20, 'image_id': 87, 'category_id': 2, 'segmentation': [[301, 303, 305, 288, 321, 261, 335, 236, 346, 214, 350, 209, 351, 205, 349, 202, 344, 203, 334, 221, 322, 244, 307, 272, 297, 290, 295, 302, 297, 310, 301, 309]], 'area': 829.5, 'bbox': [295, 202, 56, 108], 'iscrowd': 0}
{'id': 21, 'image_id': 87, 'category_id': 2, 'segmentation': [[292, 300, 288, 278, 287, 270, 283, 260, 280, 249, 276, 240, 273, 234, 270, 233, 268, 233, 266, 236, 268, 240, 272, 244, 274, 253, 276, 259, 277, 265, 280, 272, 281, 284, 285, 299, 288, 306, 291, 306, 292, 304]], 'area': 517.0, 'bbox': [266, 233, 26, 73], 'iscrowd': 0}
{'id': 22, 'image_id': 87, 'category_id': 2, 'segmentation': [[300, 279, 305, 249, 311, 233, 313, 224, 314, 211, 319, 185, 322, 172, 323, 162, 321, 155, 318, 158, 314, 168, 311, 189, 306, 217, 299, 228, 296, 237, 296, 245, 296, 254, 295, 260, 291, 279, 290, 289, 293, 295, 295, 293, 299, 287]], 'area': 1177.0, 'bbox': [290, 155, 33, 140], 'iscrowd': 0}
{'id': 23, 'image_id': 87, 'category_id': 2, 'segmentation': [[311, 308, 311, 299, 314, 292, 315, 286, 315, 282, 311, 282, 307, 284, 303, 294, 301, 303, 302, 308, 306, 307]], 'area': 235.5, 'bbox': [301, 282, 14, 26], 'iscrowd': 0}
#Weird values
{'id': 24, 'image_id': 27, 'category_id': 2, 'segmentation': {'size': [618, 561], 'counts': 'of[56Tc00O2O000001O00000OXjP5'}, 'area': 71, 'bbox': [284, 326, 10, 8], 'iscrowd': 0}
{'id': 25, 'image_id': 27, 'category_id': 1, 'segmentation': {'size': [618, 561], 'counts': 'fga54Pc0<H4L4M2O2M3M2N2N3N1N2N101N101O00000O10000O1000000000000000000000O100O100O2N1O1O2N2N3L4M3MdRU4'}, 'area': 1809, 'bbox': [294, 294, 46, 47], 'iscrowd': 0}
#Normal values again
{'id': 26, 'image_id': 61, 'category_id': 1, 'segmentation': [[285, 274, 285, 269, 281, 262, 276, 259, 271, 256, 266, 255, 257, 261, 251, 267, 251, 271, 250, 280, 251, 286, 254, 292, 258, 296, 261, 296, 265, 294, 272, 291, 277, 287, 280, 283, 283, 278]], 'area': 1024.0, 'bbox': [250, 255, 35, 41], 'iscrowd': 0}
{'id': 27, 'image_id': 61, 'category_id': 2, 'segmentation': [[167, 231, 175, 227, 180, 226, 188, 226, 198, 228, 215, 235, 228, 239, 235, 243, 259, 259, 255, 261, 252, 264, 226, 249, 216, 244, 203, 238, 194, 235, 184, 234, 171, 235, 167, 233]], 'area': 782.5, 'bbox': [167, 226, 92, 38], 'iscrowd': 0}
{'id': 28, 'image_id': 61, 'category_id': 2, 'segmentation': [[279, 186, 281, 188, 281, 192, 280, 195, 278, 200, 274, 210, 271, 218, 267, 228, 266, 233, 266, 236, 265, 239, 264, 256, 261, 257, 257, 259, 255, 244, 256, 240, 256, 238, 257, 234, 259, 227, 264, 216, 267, 205, 271, 195, 274, 190]], 'area': 593.0, 'bbox': [255, 186, 26, 73], 'iscrowd': 0}
{'id': 29, 'image_id': 61, 'category_id': 2, 'segmentation': [[264, 245, 267, 239, 269, 236, 276, 232, 280, 230, 285, 227, 287, 227, 288, 229, 287, 232, 284, 234, 282, 237, 280, 239, 276, 241, 274, 246, 271, 254, 269, 254, 266, 254, 264, 254]], 'area': 264.0, 'bbox': [264, 227, 24, 27], 'iscrowd': 0}
Find here all you need:
Interface for manipulating masks stored in RLE format
"RLE is a simple yet efficient format for storing binary masks. RLE first divides a vector (or vectorized image) into a series of piecewise
constant regions and then for each piece simply stores the length of that piece. For example, given M=[0 0 1 1 1 0 1] the RLE counts would be [2 3 1 1], or for M=[1 1 1 1 1 1 0] the counts would be [0 6 1] (note that the odd counts are always the numbers of zeros).
Instead of storing the counts directly, additional compression is achieved with a variable bitrate representation based on a common scheme called LEB128."
So, basically you can have the mask annotated as:
A polygon standard coco-json format (x,y,x,y,x,y, etc.),
A binary mask (image png)
An RLE encoded format.
All three are the same, but you need to convert them in the required format sometimes (in case your DL library doesn't support all of them, or converts them for you).

Get Object insinde JSON Object

I am pretty new to JSON and need to get an Object inside a list of JSON Objects.
This is my data structure and code so far:
{
"nhits": 15,
"parameters": {
"dataset": "100073",
"timezone": "UTC",
"q": "timestamp:[2021-02-21T23:00:00Z TO 2021-03-08T22:59:59Z]",
"rows": 10,
"start": 0,
"sort": [
"timestamp"
],
"format": "json",
"facet": [
"timestamp"
]
},
"records": [
{
"datasetid": "100073",
"recordid": "a1252522b7820edd98eb464811953d0f6ba56458",
"fields": {
"week": 10,
"ncumul_conf": 9971,
"current_quarantined": 506,
"timestamp": "2021-03-08T09:30:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9627,
"ndiff_conf": 4,
"current_quarantined_total": 623,
"current_hosp_resident": 13,
"ncumul_deceased": 192,
"current_isolated": 152,
"current_hosp": 19,
"ndiff_released": 10,
"current_hosp_non_resident": 6,
"current_quarantined_riskareatravel": 117,
"time": "10:30",
"date": "2021-03-08",
"ndiff_deceased": 0,
"current_icu": 5,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "c1a9f3fd45008ef3c140e446303ab3c2906166e0",
"fields": {
"week": 9,
"ncumul_conf": 9967,
"current_quarantined": 468,
"timestamp": "2021-03-07T11:40:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9617,
"ndiff_conf": 13,
"current_quarantined_total": 646,
"current_hosp_resident": 14,
"ncumul_deceased": 192,
"current_isolated": 158,
"current_hosp": 20,
"ndiff_released": 16,
"current_hosp_non_resident": 6,
"current_quarantined_riskareatravel": 178,
"time": "12:40",
"date": "2021-03-07",
"ndiff_deceased": 0,
"current_icu": 5,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "3668aa9ae4f9cf73890ad8c7f13efef7246cc461",
"fields": {
"week": 9,
"ncumul_conf": 9954,
"current_quarantined": 417,
"timestamp": "2021-03-06T11:20:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9601,
"ndiff_conf": 22,
"current_quarantined_total": 602,
"current_hosp_resident": 13,
"ncumul_deceased": 192,
"current_isolated": 161,
"current_hosp": 19,
"ndiff_released": 23,
"current_hosp_non_resident": 6,
"current_quarantined_riskareatravel": 185,
"time": "12:20",
"date": "2021-03-06",
"ndiff_deceased": 0,
"current_icu": 5,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "96a2bfde464cb4664ae8b16723960a7141800e56",
"fields": {
"week": 9,
"ncumul_conf": 9932,
"current_quarantined": 345,
"timestamp": "2021-03-05T09:50:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9578,
"ndiff_conf": 25,
"current_quarantined_total": 550,
"current_hosp_resident": 12,
"ncumul_deceased": 192,
"current_isolated": 162,
"current_hosp": 20,
"ndiff_released": 14,
"current_hosp_non_resident": 8,
"current_quarantined_riskareatravel": 205,
"time": "10:50",
"date": "2021-03-05",
"ndiff_deceased": 0,
"current_icu": 6,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "37a9b2c6a896a7dff362b27b671c71b83f467ccd",
"fields": {
"week": 9,
"ncumul_conf": 9907,
"current_quarantined": 253,
"timestamp": "2021-03-04T09:40:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9564,
"ndiff_conf": 27,
"current_quarantined_total": 481,
"current_hosp_resident": 13,
"ncumul_deceased": 192,
"current_isolated": 151,
"current_hosp": 21,
"ndiff_released": 23,
"current_hosp_non_resident": 8,
"current_quarantined_riskareatravel": 228,
"time": "10:40",
"date": "2021-03-04",
"ndiff_deceased": 0,
"current_icu": 6,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "c7933687391ff92436f1a75503648ce9430e0baa",
"fields": {
"week": 9,
"ncumul_conf": 9880,
"current_quarantined": 241,
"timestamp": "2021-03-03T10:50:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9541,
"ndiff_conf": 13,
"current_quarantined_total": 467,
"current_hosp_resident": 15,
"ncumul_deceased": 192,
"current_isolated": 147,
"current_hosp": 23,
"ndiff_released": 15,
"current_hosp_non_resident": 8,
"current_quarantined_riskareatravel": 226,
"time": "11:50",
"date": "2021-03-03",
"ndiff_deceased": 0,
"current_icu": 7,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "dd830a16c7f18e6cc2d5f8b03f5a75437d1331d3",
"fields": {
"week": 9,
"ncumul_conf": 9867,
"current_quarantined": 197,
"timestamp": "2021-03-02T09:40:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9526,
"ndiff_conf": 28,
"current_quarantined_total": 419,
"current_hosp_resident": 15,
"ncumul_deceased": 192,
"current_isolated": 149,
"current_hosp": 22,
"ndiff_released": 27,
"current_hosp_non_resident": 7,
"current_quarantined_riskareatravel": 222,
"time": "10:40",
"date": "2021-03-02",
"ndiff_deceased": 0,
"current_icu": 7,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "4de6410562c2e0329a9395f8e7687ed098f788b6",
"fields": {
"week": 9,
"ncumul_conf": 9839,
"current_quarantined": 159,
"timestamp": "2021-03-01T09:40:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9499,
"ndiff_conf": -14,
"current_quarantined_total": 365,
"current_hosp_resident": 15,
"ncumul_deceased": 192,
"current_isolated": 148,
"current_hosp": 21,
"ndiff_released": -4,
"current_hosp_non_resident": 6,
"current_quarantined_riskareatravel": 206,
"time": "10:40",
"date": "2021-03-01",
"ndiff_deceased": 0,
"current_icu": 7,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "90006046ef1f6627c4c742520e37c99c04eb2db3",
"fields": {
"week": 8,
"ncumul_conf": 9853,
"current_quarantined": 167,
"timestamp": "2021-02-28T08:00:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9503,
"ndiff_conf": 13,
"current_quarantined_total": 358,
"current_hosp_resident": 10,
"ncumul_deceased": 192,
"current_isolated": 158,
"current_hosp": 16,
"ndiff_released": 14,
"current_hosp_non_resident": 6,
"current_quarantined_riskareatravel": 191,
"time": "09:00",
"date": "2021-02-28",
"ndiff_deceased": 0,
"current_icu": 7,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
},
{
"datasetid": "100073",
"recordid": "41c0f47f811b68f3ca393546e202b1b698e741c1",
"fields": {
"week": 8,
"ncumul_conf": 9840,
"current_quarantined": 177,
"timestamp": "2021-02-27T09:30:00+00:00",
"source": "https://www.gesundheit.bs.ch",
"ncumul_released": 9489,
"ndiff_conf": 21,
"current_quarantined_total": 359,
"current_hosp_resident": 10,
"ncumul_deceased": 192,
"current_isolated": 159,
"current_hosp": 16,
"ndiff_released": 14,
"current_hosp_non_resident": 6,
"current_quarantined_riskareatravel": 182,
"time": "10:30",
"date": "2021-02-27",
"ndiff_deceased": 0,
"current_icu": 7,
"abbreviation_canton_and_fl": "BS"
},
"record_timestamp": "2021-03-08T21:01:15.004000+00:00"
}
],
"facet_groups": [
{
"facets": [
{
"count": 15,
"path": "2021",
"state": "displayed",
"name": "2021"
}
],
"name": "timestamp"
}
]
}
To get the data in the "records" list i use:
import csv
from urllib.request import urlopen
import json
url = 'https://data.bs.ch/api/records/1.0/search/?dataset=100073&q=timestamp%3A%5B2021-02-21T23%3A00%3A00Z+TO+2021-03-08T22%3A59%3A59Z%5D&sort=timestamp&facet=timestamp'
ddict = {}
def getDataFromBS():
json_url = urlopen(url)
data = json.loads(json_url.read())
records = data['records']
getDataFromBS()
My problem now is, that i need to get the data inside the "fields" object. But i don't know how to extract it. Can anyone help me? Even if it is just a hint.
Every help will be much appreciated.
You need to return something from your function. The records field is a list of dictionaries. You can iterate over them and pull out the fields object
def getDataFromBS():
json_url = urlopen(url)
data = json.loads(json_url.read())
records = data['records']
fields = [r.get('fields') for r in records]
return fields
You could directly do data['records'][0]['fields'] after data = getDataFromBS()
Looks like you have the answer already, but here is another alternative if you jsut want to return 1 field. You could add a loop and iterate over the others
import json
with open('test.json') as json_file:
data = json.load(json_file)
print(data['records'][1]['fields'])

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