如何将每个列表列表放入python的csv列中?

问题描述:

我正在开发一个Python脚本,它从日志文件中获取数据,我需要将每种类型的数据保存到各自的列中。我正在使用正则表达式来获取数据。如何将每个列表列表放入python的csv列中?

这是我的代码的一部分,我得到这样的结果:

Click to view the image

#Getting data from log as list using regex 
fecha = re.findall('\d{4}\-\d{2}\-\d{2}', str(listaValores)) 
hora = re.findall('\d{2}\:\d{2}\:\d{2}', str(listaValores)) 

#List of lists about data obtained 
valoresFinales = [fecha, hora] 

#Putting into .csv 
with open("resultado.csv", "w") as f: 
    wr = csv.writer(f, delimiter=';') 
    wr.writerows(valoresFinales) 

我想要什么

Click to view the image

你给writerows功能列表的两个元素,所以你最终得到两行数据。

相反,你想给它像zip(fecha, hora)东西:

with open("resultado.csv", "w") as f: 
    wr = csv.writer(f, delimiter=';') 
    wr.writerows(zip(*valoresFinales)) 
+0

谢谢!!你救了我! – Sergi