[英]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.