0

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?

5
  • u'' notates unicode string object in Python 2. Commented Mar 29, 2015 at 6:01
  • I see. How do I extract the data and add that back to the file? Commented Mar 29, 2015 at 6:02
  • What do you mean "it corrupts the json"? Are you closing and re-opening the file between when you call load and dump? Commented Mar 29, 2015 at 6:04
  • I don't see those two lines in your code sample, when are they executed? Commented Mar 29, 2015 at 6:38
  • Not very "smart" solution is just replace whole the line with new line: Links: 225 => Links: 233542 Commented Mar 29, 2015 at 6:58

1 Answer 1

-1

Try this

import sys
import json
import re

fname = sys.argv[1]

openedFile = open(fname, 'r')
content = openedFile.read()
openedFile.close()

pattern = "Links: (\d+?)\nImages: (\d+?)"

matchObj = re.search(pattern, content)
if matchObj:
    openedFile = open(fname, 'w')
    newContent = {'Links': matchObj.group(1), 'Images': matchObj.group(2)}
    json.dump(newContent, openedFile)
    openedFile.close()
Sign up to request clarification or add additional context in comments.

5 Comments

doesn't json_dump() imply writing to the file as well?
Yes, json_dump() allows to write to the file. If the json file is open in r+ mode it allows you to read as well as write in the file
Then why are you suggestion the file be opened in 'r' mode?
Can you include your json file as well?
I am sorry I can't do that.

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.