1

This might be a silly question. I would like to replace from null to 'null' in a dict in python.

mydict = {"headers": {"ai5": "8fa683e59c02c04cb781ac689686db07", "debug": null, "random": **null**, "sdkv": "7.6"}, "post": {"event": "ggstart", "ts": "1462759195259"}, "params": {}, "bottle": {"timestamp": "2016-05-09 02:00:00.004906", "game_id": "55107008"}}

I can't do any string operation in python as it throws error:

NameError: name 'null' is not defined

I have a huge file of 18000 this type of data, and I can't do it manually.

Please help.

7
  • 5
    You cannot define mydict as a dictionary like that because there is no null in Python (that's why you get the "'null' is not defined" syntax error). It should be None. Perhaps you got that data as a string? Commented Mar 9, 2019 at 3:10
  • Are these values stored in a file?? Because this declaration will not work in python. Can you tell the deatils of the format in which you have the values Commented Mar 9, 2019 at 3:14
  • I am not defining it. I have a txt file which has 18000 line like this one. and i need to replace all the null to 'null' Commented Mar 9, 2019 at 3:22
  • 1
    @GauravPhagre That is JSON: parse JSON as JSON (Python has built-in support for parsing JSON.. the null will be parsed as None, the Python equivalent, in the resulting parsed objects). It is probably less ideal to attempt and copy-paste the JSON "as Python". Commented Mar 9, 2019 at 3:41
  • 1
    Is there a reason why you want a "null" string? If you parse the data properly as JSON (as mentioned by @user2864740), null automatically gets converted to None, which is much better for doing whatever else you need to do in your Python code. Commented Mar 9, 2019 at 4:24

3 Answers 3

3

You dont need to replace anything. Just load the file and convert the data to dict.

import json
import pprint

with open('x.json') as f:
    data = json.load(f)
    pprint.pprint(data)

Input (x.json)

{
  "headers": {
    "ai5": "8fa683e59c02c04cb781ac689686db07",
    "debug": null,
    "random": null,
    "sdkv": "7.6"
  },
  "post": {
    "event": "ggstart",
    "ts": "1462759195259"
  },
  "params": {},
  "bottle": {
    "timestamp": "2016-05-09 02:00:00.004906",
    "game_id": "55107008"
  }
}

Output

{'bottle': {'game_id': '55107008', 'timestamp': '2016-05-09 02:00:00.004906'},
 'headers': {'ai5': '8fa683e59c02c04cb781ac689686db07',
             'debug': None,
             'random': None,
             'sdkv': '7.6'},
 'params': {},
 'post': {'event': 'ggstart', 'ts': '1462759195259'}}
Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this. It replaces every null with 'null' and stores it into new file. Storing it into same file or writing it to new file and then replacing it with original one is upto you.

import re
f_handle = open("test.txt","r+")
f_2 = open("result.txt","w+")
for f_string in f_handle.readlines():
    print(f_string)
    f_result = re.sub(r'(?:^|\W)null(?:$|\W)',"'null'",f_string)
    print(f_result)
    f_2.write(f_result)
f_handle.close()
f_2.close()

Comments

1
import json
x = json.loads(mydict)

and then null in your dict will change to be "".

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.