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

2024-09-28 05:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python开发一个脚本,它从日志文件中获取数据,我需要将每种类型的数据保存到相应的列中。我用正则表达式来获取数据

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

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


Tags: 文件csv数据re脚本类型dataas
1条回答
网友
1楼 · 发布于 2024-09-28 05:16:32

writerows函数一个包含两个元素的列表,这样就得到了最后两行数据

相反,你想给它一些类似于^{}

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

相关问题 更多 >

    热门问题