-1

I have a .json file

f = open("/Users/manishverma/Documents/json/neighbor-districts.json",)
data=json.load(f)

pprint(data)
{'adilabad/Q15211': ['komram_bheem/Q28170184',  
                     'nanded/Q692389',  
                     'yavatmal/Q1804852',
                     'nirmal/Q28169750',  
                     'chandrapur/Q1797274'],  
 'agar_malwa/Q15732396': ['rajgarh/Q1833306',  
                          'shajapur/Q2449803',  
                          'ratlam/Q2299164',  
                          'ujjain/Q892641',  
                          'jhalawar/Q1471417'],  
 'agra/Q606343': ['etawah/Q1815288',  
                  'hathras/Q1814892',  
                  'dhaulpur/Q1207709',  
                  'bhind/Q2341700',  
                  'morena/Q2341467',  
                  'mathura/Q1773422',  
                  'etah/Q1773429',  
                  'firozabad/Q1946950',  
                  'bharatpur/Q854861'],  

Now, I want to remove the code("/Q212311") from every string.

and want to change this .json file like:

{'adilabad': ['komram_bheem',
              'nanded',
              'yavatmal',
              'nirmal',
              'chandrapur'],

Please tell how to do it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
MANISH
  • 1

1 Answers1

2

Simply use dictionary comprehension:

data = { k.split('/')[0] : [i.split('/')[0] for i in v] for k, v in data.items() }
Miquel Escobar
  • 380
  • 1
  • 6