我正在使用以下代码将python dict写入CSV文件。
my_dict['date'] = time.strftime('%d-%m-%Y')
my_dict['error_log'] = data
file_exists = os.path.isfile('build_error_deatils.csv')
with open('build_error_deatils.csv', 'a+') as f:
w = csv.DictWriter(f, my_dict.keys())
if not file_exists:
w.writeheader()
w.writerow(my_dict)
除了一大块日志数据之外,所有内容都要正确复制,如下面的字符串。
data = """
../../../../../../../src/junos/usr.sbin/l2cpd/os/l2cpd_rts_reader.c: In function 'l2cpd_vpls_iff_get_stp_index':
../../../../../../../src/junos/usr.sbin/l2cpd/os/l2cpd_rts_reader.c:129: error: 'RTM_IFF_RLT_ACTIVE_LINK' undeclared (first use in this function)
../../../../../../../src/junos/usr.sbin/l2cpd/os/l2cpd_rts_reader.c:129: error: (Each undeclared identifier is reported only once
../../../../../../../src/junos/usr.sbin/l2cpd/os/l2cpd_rts_reader.c:129: error: for each function it appears in.)
cc1: warnings being treated as errors
../../../../../../../src/junos/usr.sbin/l2cpd/os/l2cpd_rts_reader.c:129: warning: unused variable 'addi'
make[1]: *** [l2cpd_rts_reader.o] Error 1
***
"""
my_dict['error_log'] = data
我得到的数据如下。
不确定为什么错误日志会变为空。 在这里需要帮助,而不是提前...
答案 0 :(得分:0)
最后能够通过将字符串更改为原始字符串来解决此问题。
my_dict['error_log'] = repr(data)