1
我發現ValueError
和KeyError
之間存在不一致。前者將\n
視爲換行符;後者將其視爲原始文本。這種行爲是否正常/預期?Python KeyError和ValueError不一致?
TeX_dict
是一個字典,用於將部分字典鍵轉換爲TeX格式化字符串,用於生成繪圖。 part
就是這樣一個部分。以下是part
的兩個示例:
a&b
已在字典中成功拆分並轉換。a,b&c
不是。
當我提出一個ValueError
時,\n
換行符會生成一個新行。當我提出KeyError
時,他們沒有。
geo_split = lambda thing: '\&'.join([
TeX_dict[x]
for x in thing.split('&')
])
try:
TeX = geo_split(part)
except KeyError:
msg = ("You have programmed limited functionality for "
"geometric mean keys. They can only handle species "
"that are hard coded into "
"``pccv.TeX_labels.axis_labels``.\n\n" #
"The following part failed: {}\n"
).format(part)
raise ValueError(msg) # If this is KeyError, the new lines don't print.
這裏是從每一個輸出樣本:
ValueError: You have programmed limited functionality for geometric mean keys.
They can only handle species that are hard coded into
``pccv.TeX_labels.axis_labels``.
KeyError: 'You have programmed limited functionality for geometric mean keys.
They can only handle species that are hard coded into
``pccv.TeX_labels.axis_labels``.\n\nThe following part failed: a,p1&p2\n'