简体   繁体   中英

How to write datetime data in a list to a csv file in Python

Here's a basic question for you guys, but I have searched but not found the answer around this and other sites.

I want to write data collected in for example two lists, let's say every 15minutes, one list for data from a sensor and the other for the logging date and time. The problem is that I can't find the correct datetime format to be written into the csv file. Here's a short verion of my code, with random data instead of sensor data, and the currend date.time instead of waiting 15 min.

import numpy as np
from random import randint
import csv
import datetime
a=[]
b=[]
for p in range (1,20):
   i = datetime.datetime.now()
   i=i.strftime('%Y/%m/%d %H:%M:%S')
   i=str(i) #also tried without this line
   a.append(i)
   b.append(randint(0,26))  
   np.savetxt('data.csv', (a,b),delimiter=',')

Gives two errors. 'A float is required' and 'Mismatch between array dtype ('<19') and format specifier ('%.18e,%.18e,...). I would really appreciate hints on this.

try this here

if you find any issue please let me know.it's working fine for me.

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