Python3.8.10
I am creating several csv files with different numbers of items in each row. They will be used in plotly. Headers are added later using csv.writerow(headers). The first item in each row is a date-string, The rest of the items in that row are 0's (int zero) with a varying number of zeros depending on which csv I am creating. I tried list.append, and list.extend, but they do not add the zeros to the same row. I tried creating an iterator but cannot get it right. There is probably a better way using pandas or something, but I'm not that experienced. Output should look like this:
2022-01-09 18:08:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2022-01-09 18:13:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2022-01-09 18:18:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2022-01-09 18:23:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2022-01-09 18:28:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2022-01-09 18:33:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2022-01-09 18:38:18,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Thx.
def daterange(start_date, end_date,period):
# create dates every period for date range.
deltaT = timedelta(seconds=period)
while start_date < end_date:
yield start_date
start_date += deltaT
def createData(hourIncrement=24,period=30,count=2282):
#creates data for specified number of hours
end_date = datetime.now()
start_date = end_date - timedelta(hours=hourIncrement)
csvData=[]
for single_date in daterange(start_date, end_date, period):
csvData.append[single_date.strftime("%Y-%m-%d %H:%M:%S"),(0 for n in range(0,count))]
listCount=len(csvData)
logger.debug('Create CSV data count: ' + str(listCount))
return csvData
This works but not very pythonic...
end_date = datetime.now()
start_date = end_date - timedelta(hours=hourIncrement)
x=0
csvData=[]
list1=[0]*(count-1)
for single_date in daterange(start_date, end_date, period):
list0=[]
list0=[single_date.strftime("%Y-%m-%d %H:%M:%S")]
for i in list1:
list0.append(i)
csvData.append(list0)
listCount=len(csvData)
logger.debug('Create CSV data count: ' + str(listCount))
return csvData
This is a more pythonic version of that code:
from datetime import datetime, timedelta
def daterange(start_date, end_date, period):
# create dates every period for date range.
while start_date < end_date:
yield start_date
start_date += timedelta(seconds=period)
def createData(hourIncrement=24, period=30, count=2282):
# creates data for specified number of hours
start = datetime.now() - timedelta(hours=hourIncrement)
end = datetime.now()
return [[x.strftime(
"%Y-%m-%d %H:%M:%S")] + [0] * (count-1) for x in daterange(start, end, period)]
print(createData(24, 8000, 5))
->
[
['2022-01-09 22:55:06', 0, 0, 0, 0],
['2022-01-10 01:08:26', 0, 0, 0, 0],
['2022-01-10 03:21:46', 0, 0, 0, 0],
['2022-01-10 05:35:06', 0, 0, 0, 0],
['2022-01-10 07:48:26', 0, 0, 0, 0],
['2022-01-10 10:01:46', 0, 0, 0, 0],
['2022-01-10 12:15:06', 0, 0, 0, 0],
['2022-01-10 14:28:26', 0, 0, 0, 0],
['2022-01-10 16:41:46', 0, 0, 0, 0],
['2022-01-10 18:55:06', 0, 0, 0, 0],
['2022-01-10 21:08:26', 0, 0, 0, 0]
]