我可以帮助您翻译IT技术方面的中文内容。以下是需要翻译的内容:
我正在开发一个Python(3.6)项目,需要将Python字典写入JSON文件。
这是我的字典:
{'deployment_name': 'sec_deployment', 'credentials': {'type': 'type1', 'project_id': 'id_001',}, 'project_name': 'Brain', 'project_id': 'brain-183103', 'cluster_name': 'numpy', 'zone_region': 'europe-west1-d', 'services': 'Single', 'configuration': '', 'routing': ''}
我需要将 credentials
键写入一个 JSON 文件中。
以下是我的尝试:
tempdir = tempfile.mkdtemp()
saved_umask = os.umask(0o077)
path = os.path.join(tempdir)
cred_data = data['credentials']
with open(path + '/cred.json', 'a') as cred:
cred.write(cred_data)
credentials = prepare_credentials(path + '/cred.json')
print(credentials)
os.umask(saved_umask)
shutil.rmtree(tempdir)
这不是编写一个JSON格式的文件,生成的文件如下:
{
'type': 'type1',
'project_id': 'id_001',
}
这段代码使用单引号而非双引号。
dict
,请使用json
模块编写它,以便正确编码,即json.dump(cred_data, cred)
。 - zwer