![](/img/trans.png)
[英]Python: How can I take a list of lists, convert every element into strings, and return the list of lists?
[英]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.