0

Structure is as follows:

{"A": [{
            "A": "",
            "B": "2",
            "C": "3QWE",
            "D": 1800,
            "E": null,
            "F": null
        },
        {
            "A": "",
            "B": "2",
            "C": "XYT17",
            "D": 1800,
            "E": "3, 1*",
            "F": null
        },
        {
            "A": "",
            "B": "N8HTY",
            "C": "XYT17",
            "D": 1800,
            "E": "7,6,(5)**",
            "F": null
        },
        {
            "A": "",
            "B": "1AJ21",
            "C": "XYT17",
            "D": 1800,
            "E": "7,6,5",
            "F": null
        }
       ],
   "B": [{
            "A": "",
            "B": "LS231",
            "C": "XYT17",
            "D": 1800,
            "E": "1,2,3",
            "F": null
        },
        {
            "A": "",
            "B": "22GHE",
            "C": "XYT17",
            "D": 1800,
            "E": "3, 1*",
            "F": null
        }
      ]
}

I want the '*' and '(' and ')' to be removed from the value of the key 'E' if it is present The output should be as follows for "E"

i.e. "E":"7,6,(5)**" ---> "E":"7,6,5"

  "E":"3, 1*" ---> "E":"3,1"



 "E":"1,2,3" stays the same

Also if after processing "1*" i have the final output as only one digit say "1" then how can i convert it to integer before updating the value.

What i did was

x=copy.copy(item['E']) 
for char in '*':              
  x = x.replace(char,'') 
item['E']=x

This gets me the result as a string. Now if i want it to be stored as an integer value if there is only a single digit then what should be done? I tried

item['E'] = int(x)

to get ValueError: invalid literal for int() with base 10: ''

Thanks in advance

2
  • 2
    Is this structure a python dictionary or JSON? I'm wondering about the 'null' values. Commented Mar 4, 2013 at 21:01
  • Its a python dictionary dumped as json Commented Mar 5, 2013 at 5:23

3 Answers 3

4

Assuming your current structure is stored in a variable called data:

for arr in data.values():
    for item in arr:
        item['E'] = item['E'].replace('(', '').replace(')', '').replace('*', '')

This assumes the structure is consistent and "E" will always be a key in the dictionaries.

If this is a JSON string and not a Python dictionary, you will first need to parse the JSON:

import json
data = json.loads(json_string)
Sign up to request clarification or add additional context in comments.

Comments

3

This will strip anything not a digit or comma from E:

import re
for a in data.values():
    for d in a:
        d['E'] = re.sub(r'[^\d,]', '', d['E'])

Comments

0

Solved using

 x=copy.copy(item['E']) 
 for char in '()*':              
      x = x.replace(char,'') 


 z = len(x)
 if z == 1:
      item['E'] = int(x)
  else:
     item['E'] = x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.