簡體   English   中英

如何將列表的每個列表放在python的csv列中?

[英]How can I put every list of lists in a csv column in python?

我正在用python開發一個腳本,該腳本從日志文件獲取數據,我需要將每種類型的數據保存到各自的列中。 我正在使用正則表達式來獲取數據。

這是我得到此結果的代碼的一部分:

點擊查看圖片

#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)

我想要的是

點擊查看圖片

您為writerows函數提供了兩個元素的列表,因此最后得到了兩行數據。

相反,您想給它類似zip(fecha, hora)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM