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 Answer 1

0

try this here

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

Sign up to request clarification or add additional context in comments.

2 Comments

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!
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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.