gpt4 book ai didi

python - 访问数据框并打印自定义错误消息

转载 作者:行者123 更新时间:2023-12-01 03:48:19 25 4
gpt4 key购买 nike

我的字典具有 Error_IDError_Messages 映射,并且这些错误消息具有 {},以便在打印时它们可以具有动态数据

dict = {'101': 'Invalid table name {}', '102': 'Invalid pair {} and {}'}

我有这个函数,每次出现错误时我都会调用它

def print_error(error_id,error_data)
print(error_id,dict[error_id].format("sample_table")

error_id='101'

print(error_id,dict[error_id].format("sample_table"))
Invalid table name sample_table

但是对于第二个错误,我应该做什么,以便我可以在 print_error 模块中使用单个 print 语句传递两件事,以便输出如下

102 Invalid pair Sample_pair1 and Sample_pair2

最佳答案

您可以使用 python 的可迭代解包功能将可变数量的参数传递给 str.format:

def print_error(error_id,error_data):
if not isinstance(error_data, tuple): # if error_data isn't a tuple
error_data= (error_data,) # make it a tuple so we can unpack it
print(error_id,dict[error_id].format(*error_data)) # unpack the tuple

print_error('101',"sample_table")
print_error('102',('a','b'))

关于python - 访问数据框并打印自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38607202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com