Python 从JSON文件提取数据时进行编码

Python 从JSON文件提取数据时进行编码,python,json,Python,Json,我有一个简单的python脚本,如下所示 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)

我有一个简单的python脚本,如下所示

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)
消息
输出如下所示

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
我想提取数据
图像:23
链接:225
,并将其更新到脚本中相同的json文件
f

如果我要做

json_data[k] = v
json.dump(json_data,f)
它会破坏JSON文件,这意味着如果我在代码中添加上述两行代码。 做

从命令行。我得到以下错误

额外数据:第2行第1列-第2行第45376列(字符2139-47514)

我不明白输出中的“u”是什么?这是某种编码吗?如果是,我如何处理它?

试试这个

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()

u“”表示Python 2中的unicode字符串对象。我明白了。如何提取数据并将其添加回文件?什么叫“它破坏了json”?您是否在调用
load
dump
之间关闭并重新打开文件?我在代码示例中没有看到这两行代码,它们是在什么时候执行的?不是很“聪明”的解决方案就是用新行替换整行:Links:225=>Links:233542doesn json_dump()不意味着也要写入文件吗?是的,json_dump()允许写入文件。如果json文件是在r+模式下打开的,它允许您在文件中读写,那么您为什么建议在“r”模式下打开该文件?您也可以包括您的json文件吗?很抱歉,我不能这样做。
cat output.json | python -m json.tool
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()