Posts: 7
Threads: 3
Joined: May 2019
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)
Posts: 379
Threads: 2
Joined: Jan 2021
count=count+1 needs to be indented one more tab so that it's part of the while loop.
Posts: 7
Threads: 3
Joined: May 2019
Feb-03-2022, 05:12 PM
(This post was last modified: Feb-03-2022, 05:12 PM by ajitnayak87.)
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)
Posts: 379
Threads: 2
Joined: Jan 2021
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
Posts: 1,145
Threads: 114
Joined: Sep 2019
Feb-03-2022, 08:41 PM
(This post was last modified: Feb-03-2022, 08:41 PM by menator01.)
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
Posts: 7
Threads: 3
Joined: May 2019
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
Posts: 7,320
Threads: 123
Joined: Sep 2016
Feb-04-2022, 12:23 AM
(This post was last modified: Feb-04-2022, 12:23 AM by snippsat.)
(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)
Posts: 379
Threads: 2
Joined: Jan 2021
Feb-04-2022, 01:21 AM
(This post was last modified: Feb-04-2022, 01:21 AM by BashBedlam.)
(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)
Posts: 16
Threads: 1
Joined: Nov 2021
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]
|