2017-05-28 122 views
0

我正在開發一個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

回答

0

你給writerows功能列表的兩個元素,所以你最終得到兩行數據。

相反,你想給它像zip(fecha, hora)東西:

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

謝謝!!你救了我! – Sergi

相關問題