如何使用Python脚本替换JSON文件中的XML代码块的值

时间:2019-03-28 13:15:32

标签: python json xml parsing

我想创建一个解析JSON文件的Python脚本。 JSON文件中有一个XML结构。 我想用新值替换该XML结构的特定字段。 更具体地说,我尝试用我想要的新值替换“ registeredBy”字段。 我需要对多个文件执行此操作,因此需要脚本。

我是Python的新手。到目前为止,我可以打开JSON文件并创建一个新值(注释为Python代码)。我仍然尝试弄清楚如何输入XML代码块并将我想要的字段替换为新值。

欢迎阅读关于什么地方/什么地方的任何建议。

谢谢。

{
    "creationDate": 1542716832357,
    "id": "15f66daf-1fc3-46b5-9d0e-915bc8058c52",
    "payload": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><tns:service xmlns:tns=\"http://example.com\"><tns:category>data</tns:category><tns:description>Service Description.</tns:description><tns:serviceMetadata><tns:registeredAt>1542386903532</tns:registeredAt><tns:registeredBy>George Papadopoulos</tns:registeredBy></tns:serviceMetadata></tns:service>",
    "resource": {
        "creationDate": 1542386903542,
        "id": "0b5dd030-af5e-4b1a-8be3-1b5e195615e2",
        "modificationDate": 1549460188076,
        "payload": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><tns:service xmlns:tns=\"http://example.com\"><tns:category>data</tns:category><tns:description>Service Description.</tns:description><tns:serviceMetadata><tns:registeredAt>1542386903532</tns:registeredAt><tns:registeredBy>George Papadopoulos</tns:registeredBy></tns:serviceMetadata></tns:service>",
        "payloadFormat": "xml",
        "resourceTypeName": "service",
        "version": "02062019133628"
    }
}
import json

with open('this.json', 'r') as json_file:
     json_data = json.load(json_file)
     for item in json_data:
           if item['registeredBy'] in ["George Papadopoulos"]:
              item['registeredBy'] = "Nick"
with open('this.json', 'w') as file:
    json.dump(json_data, json_file, indent=2)


#with open('this.json', 'r') as json_file:
#    json_data = json.load(json_file)
#    json_data['registeredBy'] = "Nick"
#
#with open('this.json', 'w') as json_file:
#    json_file.write(json.dumps(json_data))

2 个答案:

答案 0 :(得分:0)

您可以进行字符串操作。

将json转换为字符串,然后将.replace()转换为文件:

json_data = {
    "creationDate": 1542716832357,
    "id": "15f66daf-1fc3-46b5-9d0e-915bc8058c52",
    "payload": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><tns:service xmlns:tns=\"http://example.com\"><tns:category>data</tns:category><tns:description>Service Description.</tns:description><tns:serviceMetadata><tns:registeredAt>1542386903532</tns:registeredAt><tns:registeredBy>George Papadopoulos</tns:registeredBy></tns:serviceMetadata></tns:service>",
    "resource": {
        "creationDate": 1542386903542,
        "id": "0b5dd030-af5e-4b1a-8be3-1b5e195615e2",
        "modificationDate": 1549460188076,
        "payload": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><tns:service xmlns:tns=\"http://example.com\"><tns:category>data</tns:category><tns:description>Service Description.</tns:description><tns:serviceMetadata><tns:registeredAt>1542386903532</tns:registeredAt><tns:registeredBy>George Papadopoulos</tns:registeredBy></tns:serviceMetadata></tns:service>",
        "payloadFormat": "xml",
        "resourceTypeName": "service",
        "version": "02062019133628"
    }
}



import json

jsonStr = json.dumps(json_data).replace('<tns:registeredBy>George Papadopoulos</tns','<tns:registeredBy>Nick</tns')
with open('C:/this.json', 'w') as file:
    json.dump(json.loads(jsonStr), file, indent=2)

答案 1 :(得分:0)

您想将其分解为3个步骤:

  1. 解析Json
  2. 解析XML
  3. 保存更改

首先,从json获取XML部分

import json

with open('this.json', 'r') as json_file:
     json_data = json.load(json_file)

# this is the first payload, the payload under resource seems identical 
# so we will copy the updated xml over after the change
xml = json_data['payload']

第二,更改xml数据。您可能需要安装bs4lxml软件包

from bs4 import BeautifulSoup

xml_data = BeautifulSoup(xml, 'xml')
for tag in xml_data.find_all('tns:registeredBy'):
    tag.string='0' # change '0' to whatever you want

最后,保存所做的更改

json_data['payload'] = str(xml_data)
json_data['resource']['payload'] = json_data['payload']  # we can just copy the same xml over, since they were the same before change

# write to file
with open('this.json', 'w') as json_file:
    json.dump(json_data, json_file, indent=2)