简体   繁体   中英

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

I'm developing a script in python that gets data from a log file and I need to save every type of data into a respective column. I'm using regex to obtain the data.

This is a part of my code where I get this result:

点击查看图片

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

What I want

点击查看图片

You give the writerows function a list of two elements, so you get two rows of data in the end.

Instead you want to give it something like zip(fecha, hora) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM