python-3.x - 将字典转换为保留双引号的文件
问题描述
我正在尝试将 adictionary
转换为file
需要传递给curl
命令的 a 。当我使用它按预期写入的格式进行dictionary
转储时,我需要更新其中的一些值。但是双引号被替换为单引号。有没有办法以精确的格式保留?以下是我的代码json.dump
string
dictionary
file
接收输出:
{
'type': 'taskSap',
'actions': {
'abortActions': [],
'emailNotifications': [{
'attachFile': False,
'attachStdError': False,
'attachStdOut': False,
'bcc': None,
'body': None,
}
我正在使用的代码
with open("/sbranch/CTK/SAP/hkeep/vrpoutput.json", 'w') as file:
json_string = json.dumps(mydict, default=lambda o: o.__dict__, sort_keys=True)
file.write(json_string)
解决方案
最初我从 api 调用收到的输出创建了字典。在字典中进行更改对我来说更容易。但是我改变了我的逻辑而不是字典,我已经将 api 的输出写入文件并使用正则表达式来更改我需要的字符串。
orig_string=""""type": "taskSap",
"actions": {
"abortActions": [],
"emailNotifications": [],
"setVariableActions": [345erd],
"snmpNotifications": [],
"systemOperations": []
}"""
newstring='"All is good"'
ss = re.search(r"\"setVariableActions.*?,", orig_string)
if ss:
pattern=(ss.group(0))
#print(pattern)
result=orig_string.replace(pattern,newstring)
print(result)
推荐阅读
- c++ - 可以在第一个数据对象的方法中访问另一个第二个数据对象的私有属性吗?
- css - 具有两行的可滚动网格
- python - 如何在 Python 中保持 NUT2 UPS 服务器连接处于活动状态?
- c++ - 计算文件 C++ 中有多少个数据组
- python - 食人族和传教士问题python-人工智能算法
- react-native - 如何在 React-Native (Expo) 中调整图标的大小?
- sql-server - 使用 AD DS 切换到 Azure Storage Gen2 后,SSMS 将 SQL 数据库备份和还原到 Blob 停止工作
- javascript - 如何在 Javascript 中使用 json 文件替换图像元素
- c# - 接口定义了非异步版本但实现需要调用异步方法导致接口不实现错误
- swift - 展开时 window.rootViewController nil