Python Forum
Need Help writing data into Excel format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Help writing data into Excel format
#9
Although the data can be written to file one row at a time in a loop the natural way to go for me would be to collect and save all the data then write it to a csv file in one chunk. That is the purpose of the csv modules csvwriter.writerows() and why it is often seen in the following format

with open('test.csv', "a+", newline ="") as csvfile:
	csvwriter = csv.writer(csvfile)
	csvwriter.writerows(fields)
	csvwriter.writerows(test_list)
The writerows expects a list of lists as a parameter so the format of the header would be similar to this

fields = [['SLNo', 'Date','Time', 'RData', 'BData','GData','IRdata']]
The list of lists that contain the data is not quite as straight forward but can be built as the data is collected, using your original count of 10 to simulate data collection this is my take on collecting 10 rows of data and packing them in a list of lists then writing the header and data to file in one go

from datetime import datetime

import csv

fields = [['SLNo', 'Date','Time', 'RData', 'BData','GData','IRdata']]
test_list=[]
data_string=""
count=0

while(count<10):
	print("Counter:",count)
	now = datetime.now()
	Sln0=count
	date_Format=now.strftime('%Y/%m/%d ')
	current_time = now.strftime("%I:%M:%S")

	data_string=(str(Sln0) + "," + date_Format + "," + current_time)

	test_list.append([])  #creates a list of lists
	test_list[count]=data_string.split(",") 
	count=count+1

with open('test.csv', "a+",newline="") as csvfile:
	csvwriter = csv.writer(csvfile)
	csvwriter.writerows(fields)
	csvwriter.writerows(test_list)
[color=#?][/color]
Reply


Messages In This Thread
RE: Need Help writing data into Excel format - by Jeff_t - Feb-04-2022, 03:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 415 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Export data from PDF as tabular format zinho 5 820 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,244 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,190 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 2,028 Dec-12-2022, 08:22 PM
Last Post: jh67
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,221 Dec-06-2022, 11:09 AM
Last Post: mg24
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,823 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Appending a row of data in an MS Excel file azizrasul 3 1,266 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Moving data from one Excel to another and finding maximum profit azizrasul 7 1,582 Oct-06-2022, 06:13 PM
Last Post: azizrasul
  Create a function for writing to SQL data to csv mg24 4 1,269 Oct-01-2022, 04:30 AM
Last Post: mg24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020