如何根据用户的输入为字典分配名称
How to assign a name to a dictionary based on input from a user
我们如何为字典指定一个取自用户输入的名称,并将该字典保存到一个 txt 文件中,以便我们可以按名称搜索它并将输出打印给用户?
我现在在这里:
有什么想法吗?
import sys
import pathlib
'''Arg-V's should be in following order <app.py> <action> <nick_name> <name> <phone> <email>'''
if str(sys.argv[1]).lower == 'add':
current = {'Name': sys.argv[3], 'Phone Number': sys.argv[4], 'Email': sys.argv[5]}
with open('contacts.txt', 'w') as f:
f.write(current)
根据Naming Lists Using User Input:
An indirect answer is that, as several other users have told you, you don't want to let the user choose your variable names, you want to be using a dictionary of lists, so that you have all the lists that users have created together in one place.
import json
name = input('name/s of dictionary/ies : ')
names = {}
name = name.split()
print(name)
for i in name:
names[i]={}
print(names)
for i in names:
print(i,'--------->', names[i])
for i in names:
names[i] = '1'*len(i)
for i in names:
with open(i+'.txt', 'w+') as file:
file.write('prova : '+json.dumps(names[i]))
我们如何为字典指定一个取自用户输入的名称,并将该字典保存到一个 txt 文件中,以便我们可以按名称搜索它并将输出打印给用户? 我现在在这里:
有什么想法吗?
import sys
import pathlib
'''Arg-V's should be in following order <app.py> <action> <nick_name> <name> <phone> <email>'''
if str(sys.argv[1]).lower == 'add':
current = {'Name': sys.argv[3], 'Phone Number': sys.argv[4], 'Email': sys.argv[5]}
with open('contacts.txt', 'w') as f:
f.write(current)
根据Naming Lists Using User Input:
An indirect answer is that, as several other users have told you, you don't want to let the user choose your variable names, you want to be using a dictionary of lists, so that you have all the lists that users have created together in one place.
import json
name = input('name/s of dictionary/ies : ')
names = {}
name = name.split()
print(name)
for i in name:
names[i]={}
print(names)
for i in names:
print(i,'--------->', names[i])
for i in names:
names[i] = '1'*len(i)
for i in names:
with open(i+'.txt', 'w+') as file:
file.write('prova : '+json.dumps(names[i]))