1

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.

1 Answers1

0

try this here

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

  • I still want to save to a csv file, but what I did was take the advice on adding (..., fmt="%s"). And it worked! It was also easy to imort to an excel and Libre Calc. Thanks a lot! – andregozaemon Dec 26 '17 at 14:00
  • 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)) DAT = np.column_stack((a, b)) np.savetxt('data.csv', DAT,delimiter=',' ,fmt='%s') – Nitin Vaishwade Dec 28 '17 at 08:40