Python 3.x 我怎样才能避免一大堆撇号呢?
使用Python 3.7,我有一个看起来令人困惑的嵌套字典:Python 3.x 我怎样才能避免一大堆撇号呢?,python-3.x,dictionary,Python 3.x,Dictionary,使用Python 3.7,我有一个看起来令人困惑的嵌套字典: dict = \ { 'HBL_Posts': {'vNames':[ 'id_no', 'display_msg_no', 'thread', 'headline', 'category', 'author', 'auth_addr', 'author_pic_line', 'postbody', 'last_msg_no
dict = \
{
'HBL_Posts':
{'vNames':[ 'id_no', 'display_msg_no', 'thread', 'headline', 'category', 'author',
'auth_addr', 'author_pic_line', 'postbody',
'last_msg_no', 'mf_lnk', 'subject_header' ],
'data_fname':'_Posts_plain.htm', 'tpl_fname':'_Posts_tpl.htm', 'addrs_fname':'_addrs.csv' },
'MOTM':
{'vNames':[ 'work_month', 'zoom', 'zoom_id', 'headline', 'description', 'subject_header' ],
'data_fname':'_Posts_plain.htm', 'tpl_fname':'_Posts_tpl.htm', 'addrs_fname':'_addrs.csv'},
'MOTM recording':
{'vNames':[ 'topic', 'description', 'wDate', 'box', 'chat'],
'data_fname':'_Recording_data.htm', 'tpl_fname':'_Recording_tpl.htm', 'addrs_fname':'_addrs.csv'},
'Enticement':
{'vNames':[ 'enticing_post', 'headline', 'hb_preface', 'postscript'],
'data_fname':'_Entice_data.htm', 'tpl_fname':'_Entice_tpl.htm', 'addrs_fname':'_entice.csv'}
}
如果我最初将每个变量设置为自己的名称,比如:HBL_Posts='HBL_Posts',我可以用更清晰、更不容易打字的代码来代替:
dict = \
{
HBL_Posts:
{vNames:[ id_no, display_msg_no, thread, headline, category, author,
auth_addr, author_pic_line, postbody,
last_msg_no, mf_lnk, subject_header ],
data_fname:_Posts_plain.htm, tpl_fname:_Posts_tpl.htm, addrs_fname:_addrs.csv },
MOTM:
{vNames:[ work_month, zoom, zoom_id, headline, description, subject_header ],
data_fname:_Posts_plain.htm, tpl_fname:_Posts_tpl.htm, addrs_fname:_addrs.csv},
MOTM recording:
{vNames:[ topic, description, wDate, box, chat],
data_fname:_Recording_data.htm, tpl_fname:_Recording_tpl.htm, addrs_fname:_addrs.csv},
Enticement:
{vNames:[ enticing_post, headline, hb_preface, postscript],
data_fname:_Entice_data.htm, tpl_fname:_Entice_tpl.htm, addrs_fname:_entice.csv}
}
事实上,我是通过一次一个地完成所有必要的任务来完成的。但这和原始词典一样复杂,只有撇号。我想要的是一个函数,它能让我干净利落、经济实惠地完成这项工作
def self_name(s):
[?????]
然后我可以有一个所有变量的列表,vars_lst,并通过它循环设置每个变量为其本身的文本版本:
for item in vars_lst:
item = self_name(item)
为了避免在设置vars_lst时使用撇号,我接受这样做:
HBL_Posts = vNames = id_no = . . . = ''
经过无数个小时的努力,我一直无法为self_name函数提供所需的代码。我怎样才能做到这一点,或者我怎样才能找到避免这么多撇号的另一种方法呢?像JSON一样缩进:
{
"HBL_Posts": {
"vNames": [
"id_no",
"display_msg_no",
"thread",
"headline",
"category",
"author",
"auth_addr",
"author_pic_line",
"postbody",
"last_msg_no",
"mf_lnk",
"subject_header"
],
"data_fname": "_Posts_plain.htm",
"tpl_fname": "_Posts_tpl.htm",
"addrs_fname": "_addrs.csv"
},
"MOTM": {
"vNames": [
"work_month",
"zoom",
"zoom_id",
"headline",
"description",
"subject_header"
],
"data_fname": "_Posts_plain.htm",
"tpl_fname": "_Posts_tpl.htm",
"addrs_fname": "_addrs.csv"
},
"MOTM recording": {
"vNames": [
"topic",
"description",
"wDate",
"box",
"chat"
],
"data_fname": "_Recording_data.htm",
"tpl_fname": "_Recording_tpl.htm",
"addrs_fname": "_addrs.csv"
},
"Enticement": {
"vNames": [
"enticing_post",
"headline",
"hb_preface",
"postscript"
],
"data_fname": "_Entice_data.htm",
"tpl_fname": "_Entice_tpl.htm",
"addrs_fname": "_entice.csv"
}
}
甚至可以将其存储在.json
文件中,并通过以下方式加载:
import json
with open('my_file.json', 'r') as f:
my_dict = json.load(f)
JSON对大多数人来说很容易阅读,缩进也很容易看到。另外,它很容易保存和读取文件,所以您不必把代码弄得乱七八糟
供参考:
您可以使用以下方法打印字典:
import json
my_dict = ...
print(json.dumps(my_dict, indent=4))
这就是我打印你的字典的方式。你是否考虑过使用
mydict=dict(keyword=value)
而不是初始化为mydict={'keyword':value}
?“更清晰,更不容易打字”。我非常不同意。总的来说,我还建议将其分解,并分别定义每个子目录。有引号并不是那么容易混淆。令人困惑的是它的结构和呈现方式。使用json.dumps
和indent
使其看起来很简单。您是否考虑过在yaml中定义它,然后将其转换为适当的python字典?所有这些想法都是好主意。我特别喜欢将怪物从程序文件中取出,并导入json文件。但是我从来没有听说过像mydict=dict{keyword=value}这样做的可能性。我不知道这是怎么回事,但我会一直玩下去,直到我这么做。好吧,我放弃Daniel Lenz所说的“使用mydict=dict(关键字=value)而不是初始化为mydict={'keyword':value}”的意思???当我尝试做任何类似dict(关键字=值)的事情时,我会得到“TypeError:“dict”对象不可调用”。我不知道不可调用意味着什么,但更重要的是,我看不到初始化字典的任何选项。他的意思是你可以做d=dict(a=2,b=3)
。尽管如此,我还是不推荐这个方向。正如孩子在老鲍勃和雷的例行公事中对“科学先生”说的那样,“哎呀,我更了解这一点。”一本字典怎么能用括号来设置呢?我猜您首先要做一些类似于dict={}的事情,在dict中设置一些东西,然后d为其创建一个元组。但是语法对我来说毫无意义。我试着输入d=dict(a=2,b=3),但解释器拒绝了语法。显然,没有人认为有一种通用的方法可以实现OP的功能:将对象的值设置为与其名称相同。(例如,myvar='myvar')