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
#1
I have the below code. I have one counter. if counter less than value set value It should write the value into excel.
in below code I could see counter printing 0 and hangs

The filed column should be written only once
I wanted to write into excel per 1S time sleep
Counter has to increment every time and save into SL no


I have used pyscripter 4

import csv
import datetime
import time
from datetime import datetime

filename = "TestFormat.csv"
fields = ['SLNo', 'Date','Time', 'RData', 'BData','GData','IRdata']


def main():
   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")
        with open(filename, 'a+') as csvfile:
            csvwriter = csv.writer(csvfile)
            csvwriter.writerow(fields)
            csvwriter.writerow([Sln0,date_Format,current_time])
        csvfile.close()
   count=count+1

if __name__ == '__main__':
    main()
    time.sleep(1)
Reply
#2
count=count+1needs to be indented one more tab so that it's part of thewhileloop.
Reply
#3
I have modified the code. Now it properly counting.

I need this statement print only once and later append .
SLNo Date Time RData BData GData IRdata
And it's giving extra row on top and bottom. Means jumping row one while writing. It must be continous






Output:
SLNo Date Time RData BData GData IRdata 0 03-02-2022 10.36.22 SLNo Date Time RData BData GData IRdata 1 03-02-2022 10.36.22 SLNo Date Time RData BData GData IRdata 2 03-02-2022 10.36.22 SLNo Date Time RData BData GData IRdata
import csv
import datetime
import time
from datetime import datetime

filename = "TestFormat.csv"
fields = ['SLNo', 'Date','Time', 'RData', 'BData','GData','IRdata']


def main():
   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")
        with open(filename, 'a+') as csvfile:
            csvwriter = csv.writer(csvfile)
            csvwriter.writerow(fields)
            csvwriter.writerow([Sln0,date_Format,current_time])
        csvfile.close()
        count=count+1

if __name__ == '__main__':
    main()
    time.sleep(1)
Reply
#4
It seems to work correctly for me.
Output:
SLNo,Date,Time,RData,BData,GData,IRdata 0,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 1,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 2,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 3,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 4,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 5,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 6,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 7,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 8,2022/02/03 ,11:29:25 SLNo,Date,Time,RData,BData,GData,IRdata 9,2022/02/03 ,11:29:25
Reply
#5
Is this what you want? I do not really understand the sleep part of your code.

import csv
from datetime import datetime

filename = 'testformat.csv'
fields = ['SLNo', 'Date', 'Time', 'RData', 'BData', 'GData', 'IRData']
date_format = datetime.now().strftime('%Y/%m/%d')
current_time = datetime.now().strftime('%I:%M:%S')

def main():
    with open(filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow(fields)
        for i in range(10):
            csvwriter.writerow([i, date_format, current_time])

if __name__ == '__main__':
    main()
Output:
SLNo,Date,Time,RData,BData,GData,IRData 0,2022/02/03,01:09:09 1,2022/02/03,01:09:09 2,2022/02/03,01:09:09 3,2022/02/03,01:09:09 4,2022/02/03,01:09:09 5,2022/02/03,01:09:09 6,2022/02/03,01:09:09 7,2022/02/03,01:09:09 8,2022/02/03,01:09:09 9,2022/02/03,01:09:09
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
I have seen Still Its jumping one row printing into Excel. I need continuous

Its writing like this
SLNo Date Time RData BData GData IRData
Empty Row
0 04-02-2022 02:59:53

Empty row
0 04-02-2022 02:59:53

Empty row

I need continuous location to be printed
Reply
#7
(Feb-03-2022, 09:32 PM)ajitnayak87 Wrote: I have seen Still Its jumping one row printing into Excel. I need continuous
Take into Pandas first then is easier to change stuff and the DataFrame will look like Excel.
When it look right use df.to_excel().

Example,and i use output data from menator01.
import pandas as pd
from io import StringIO

data = StringIO('''\
SLNo,Date,Time,RData,BData,GData,IRData
0,2022/02/03,01:09:09
1,2022/02/03,01:09:09
2,2022/02/03,01:09:09
3,2022/02/03,01:09:09
4,2022/02/03,01:09:09
5,2022/02/03,01:09:09
6,2022/02/03,01:09:09
7,2022/02/03,01:09:09
8,2022/02/03,01:09:09
9,2022/02/03,01:09:09''')

df = pd.read_csv(data, sep=',')
>>> df
   SLNo        Date      Time  RData  BData  GData  IRData
0     0  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
1     1  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
2     2  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
3     3  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
4     4  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
5     5  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
6     6  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
7     7  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
8     8  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
9     9  2022/02/03  01:09:09    NaN    NaN    NaN     NaN
>>> 
>>> df = df.dropna(axis='columns')
>>> df
   SLNo        Date      Time
0     0  2022/02/03  01:09:09
1     1  2022/02/03  01:09:09
2     2  2022/02/03  01:09:09
3     3  2022/02/03  01:09:09
4     4  2022/02/03  01:09:09
5     5  2022/02/03  01:09:09
6     6  2022/02/03  01:09:09
7     7  2022/02/03  01:09:09
8     8  2022/02/03  01:09:09
9     9  2022/02/03  01:09:09
>>> 
>>> df.to_excel("output.xlsx", index=False)
Reply
#8
(Feb-03-2022, 09:32 PM)ajitnayak87 Wrote: I have seen Still Its jumping one row printing into Excel. I need continuous
Run this and let me know if you are still having the problem.
import csv
from datetime import datetime
 
filename = 'testformat.csv'
fields = ['SLNo', 'Date', 'Time', 'RData', 'BData', 'GData', 'IRData']
date_format = datetime.now().strftime('%Y/%m/%d')
current_time = datetime.now().strftime('%I:%M:%S')
 
def main():
	with open(filename, 'w') as csvfile:
		csvwriter = csv.writer(csvfile)
		csvwriter.writerow(fields)
		for i in range(10):
			csvwriter.writerow([i, date_format, current_time])

def read_back () :
	with open (filename) as csvfile :
		data = csv.reader (csvfile)
		for row in data:
			print(row)

if __name__ == '__main__':
	main()
	read_back ()
Output:
['SLNo', 'Date', 'Time', 'RData', 'BData', 'GData', 'IRData'] ['0', '2022/02/03', '06:16:46'] ['1', '2022/02/03', '06:16:46'] ['2', '2022/02/03', '06:16:46'] ['3', '2022/02/03', '06:16:46'] ['4', '2022/02/03', '06:16:46'] ['5', '2022/02/03', '06:16:46'] ['6', '2022/02/03', '06:16:46'] ['7', '2022/02/03', '06:16:46'] ['8', '2022/02/03', '06:16:46'] ['9', '2022/02/03', '06:16:46']

Attached Files

Thumbnail(s)
   
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 247 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Export data from PDF as tabular format zinho 5 702 Nov-11-2023, 08:23 AM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,050 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,102 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 1,878 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,042 Dec-06-2022, 11:09 AM
Last Post: mg24
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,712 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Appending a row of data in an MS Excel file azizrasul 3 1,175 Nov-06-2022, 05:17 PM
Last Post: azizrasul
  Moving data from one Excel to another and finding maximum profit azizrasul 7 1,471 Oct-06-2022, 06:13 PM
Last Post: azizrasul
  Create a function for writing to SQL data to csv mg24 4 1,153 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