如何用python将dict列表的内容写入文件
我从一个db调用中获取作为dict列表的输出,我想将输出写入一个带有一些配置的文件中。 范例 我的日志如何用python将dict列表的内容写入文件,python,Python,我从一个db调用中获取作为dict列表的输出,我想将输出写入一个带有一些配置的文件中。 范例 我的日志 pattern|message abc|This is not possible def|The parameter provided application is invalid ijk|Expected value for debugging 谢谢我的标题基于表的输出,它将是动态的。怎么做@XI编写包含标题的变量,而不是编写文字字符串。。。 with open('my.log', 'w'
pattern|message
abc|This is not possible
def|The parameter provided application is invalid
ijk|Expected value for debugging
谢谢我的标题基于表的输出,它将是动态的。怎么做@XI编写包含标题的变量,而不是编写文字字符串。。。
with open('my.log', 'w') as log:
log.write('pattern|message\n')
for line in my_dict:
log.write('{pattern}|{message}\n'.format(**line))
pattern|message
abc|This is not possible
def|The parameter provided application is invalid
ijk|Expected value for debugging