我如何在Python / JSON中的数据之间插入我的输入数据?

huangapple go评论65阅读模式
英文:

How can I insert data between the data with my inputs in python / json?

问题

我有输入变量 ab,我想将它们插入到我的 JSON 文件中。

import json

with open("intents_kor.json") as file:
    data = json.load(file)

a = input('输入 a:')
b = input('输入 b:')

data['intents'].append({'tag': 'not_in_json', 'patterns': [a], 'responses': [b], 'context_set': ''})

我想将我的输入变量 ab 放入 append() 中,应该如何做?

英文:

I have inputs a and b, and I want to insert them into my json file.

import json

with open("intents_kor.json") as file:
data = json.load(file)

a = input('input a : ')
b = input('input b : ')

data['intents'].append({'tag': 'not_in_json', 'patterns': ['a'], 'responses': ['b'], 'context_set': ''})

I want to put my inputs a and b to a and b with append().
How can I do this?

答案1

得分: 1

这是解决方案:

import json

with open("intents_kor.json") as file:
    data = json.load(file)

a = input('输入 a:')
b = input('输入 b:')

data['intents'] = {'tag': 'not_in_json', 'patterns': [a], 'responses': [b], 'context_set': ''}
print(data)

请注意,我只翻译了代码部分,没有包括问题中的其他内容。

英文:

Here is the solution:

import json

with open("intents_kor.json") as file:
    data = json.load(file)

a = input('input a : ')
b = input('input b : ')

data['intents']={'tag': 'not_in_json', 'patterns': [a], 'responses': [b], 'context_set': ''}
print(data)

huangapple
  • 本文由 发表于 2020年1月3日 14:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定