Need Help writing data into Excel format - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need Help writing data into Excel format (/thread-36270.html) |
Need Help writing data into Excel format - ajitnayak87 - Feb-03-2022 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) RE: Need Help writing data into Excel format - BashBedlam - Feb-03-2022 count=count+1 needs to be indented one more tab so that it's part of thewhile loop.
RE: Need Help writing data into Excel format - ajitnayak87 - Feb-03-2022 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
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) RE: Need Help writing data into Excel format - BashBedlam - Feb-03-2022 It seems to work correctly for me.
RE: Need Help writing data into Excel format - menator01 - Feb-03-2022 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()
RE: Need Help writing data into Excel format - ajitnayak87 - Feb-03-2022 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 RE: Need Help writing data into Excel format - snippsat - Feb-04-2022 (Feb-03-2022, 09:32 PM)ajitnayak87 Wrote: I have seen Still Its jumping one row printing into Excel. I need continuousTake 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) RE: Need Help writing data into Excel format - BashBedlam - Feb-04-2022 (Feb-03-2022, 09:32 PM)ajitnayak87 Wrote: I have seen Still Its jumping one row printing into Excel. I need continuousRun 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 ()
RE: Need Help writing data into Excel format - Jeff_t - Feb-04-2022 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] |