data={'name':"1.1.1.1",'subnet'="1.1.1.1/32",'id'=3,'env'="PROD"}
现在只改变键是名称和子网,id和环境保持不变。而传递名称和子网值必须在"之内。
像这样的输入文件1.1.1.1 1.1.1.1/32
with open("file1.txt") as f:
for i in f:
value_string=str(i)
data={'name':'+value_string+','subnet'="1.1.1.1/32",'id'=3,'env'="PROD"}
pprint(data)
但是它在"中没有价值以及如何在子网中传递值。
我想你在寻找类似
的东西with open("file1.txt") as f:
for line in f:
ip, subnet = line.strip().split(None, 1)
data = {"name": ip, "subnet": subnet, "id": 3, "env": "PROD"}
pprint(data)
请参阅下面的解决方案:
data = dict()
with open('file.txt', 'r') as f:
for line in f:
data['name'], data['subnet'], data['id'], data['env'] = line.split(' ')[0], line.split(' ')[1], '3', 'PROD'