I have a simple python script as shown below.
with open(fname, 'r+') as f:
json_data = json.load(f)
message = json_data['Info']
for line in message.split('<br>'):
if(len(line) < 25):
print(line)
if ':' in line:
k,v = line.strip().split(':')
print(k,v)
I get k,v in the following format
(u'Images', u' 23')
(u'Links', u' 225')
The message output looks as below.
Title: Worlds best websit | mywebsite.com
Links: 225
Images: 23
Browser: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Ubuntu Chromium/41.0.2272.76 Chrome/41.0.2272.76 Safari/537.36
CPUs: 8
I want to extract the data Images:23 and Links:225 and update that to the same json file f in the script.
If I were to do
json_data[k] = v
json.dump(json_data,f)
it corrupts the JSON file.Meaning If I add the above two lines to my code. and do
cat output.json | python -m json.tool
from the command line.I get the following error.
Extra data: line 2 column 1 - line 2 column 45376 (char 2139 - 47514)
I don't understanding what is 'u' in the output? Is it some kind of encoding? If yes how do I process it?
loadanddump?