Why is date being written as float in csv file
Question:
I am trying to save some data in csv file. Some of the date formats are changed to float.
This is data:
results = [{'Event ID': 15, 'Time Start': '2022/05/04 17:75', 'Time End': '2022/05/04 18:00''}
{'Event ID': 15, 'Time Start': '2022/05/04 18:00', 'Time End': '2022/05/04 20:50'}
{'Event ID': 0, 'Time Start': '2022/05/06 16:50', 'Time End': '2022/05/06 17:00'}
{'Event ID': 4, 'Time Start': '2022/05/09 15:00', 'Time End': '2022/05/09 15:50'}
{'Event ID': 14, 'Time Start': '2022/06/13 07:75', 'Time End': '2022/06/13 08:00'}
{'Event ID': 4, 'Time Start': '2022/06/15 09:00', 'Time End': '2022/06/15 10:50'}
{'Event ID': 14, 'Time Start': '2022/06/16 02:75', 'Time End': '2022/06/16 03:00'}]
The code to save it as csv is as shown below
csv_columns = ['Event ID', 'Time Start', 'Time End']
csv_file = "ets_results.csv"
try:
with open(csv_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in results:
print(data)
writer.writerow(data)
except IOError:
print("I/O error")
The output of the csv file looks like this
Answers:
I am trying to save some data in csv file. Some of the date formats are changed to float.
This is data:
results = [{'Event ID': 15, 'Time Start': '2022/05/04 17:75', 'Time End': '2022/05/04 18:00''}
{'Event ID': 15, 'Time Start': '2022/05/04 18:00', 'Time End': '2022/05/04 20:50'}
{'Event ID': 0, 'Time Start': '2022/05/06 16:50', 'Time End': '2022/05/06 17:00'}
{'Event ID': 4, 'Time Start': '2022/05/09 15:00', 'Time End': '2022/05/09 15:50'}
{'Event ID': 14, 'Time Start': '2022/06/13 07:75', 'Time End': '2022/06/13 08:00'}
{'Event ID': 4, 'Time Start': '2022/06/15 09:00', 'Time End': '2022/06/15 10:50'}
{'Event ID': 14, 'Time Start': '2022/06/16 02:75', 'Time End': '2022/06/16 03:00'}]
The code to save it as csv is as shown below
csv_columns = ['Event ID', 'Time Start', 'Time End']
csv_file = "ets_results.csv"
try:
with open(csv_file, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in results:
print(data)
writer.writerow(data)
except IOError:
print("I/O error")
The output of the csv file looks like this