Generating multiple CSV files on hourly basis in Python -
I have a python module, called HariseSecGeneration_JPi. I have some data that is generated on hourly basis. Sample.txt is Sample.txt is sampled in data here: -
2014-07-24 15: 00: 00,1,1,1,1,1001 2014-07-24 15: 01: 00,1,1,1,1,1001 2014-07-24 15: 02: 00,1,1,1,1001 2014-07-24 15:15, 00,1,1,1, 1, 1001 2014-07-24 15: 16: 00,1,1,1,1,1001 2014-07-24 15:17-17: 00,1,1,1,1001 2014-07-24 15:30: 00,1,1,1,1,1001 2014-07-24 15: 31: 00,1,1,1,1001 2014-07-24 15: 32,101,1,1, 1,1001 2014 -07-24 15: 45: 00,1,1,1,1,1001 2014-07-24 15: 46: 00,1,1,1,1,1001 2014-07-24 15:47:47, 1,1,1,1,1001 As you can see that 4 intervals are 00-15, 15-30, 30, 45 and 45-59 and the next hour starts And so on I am writing code that will read data in this txt file and generate 4 CSV files for each hour in one day. Therefore, the analysis of the above data should be generated by 4 CSV files, which should be named Conference 2014-07-24 15: 00 CSV, which contains 15:00 and 15:15, 2014-07-24 15:15 data containing CSV 15:15 And data for every hour at 15:30 and so on. Python code should do all this.
Here's my current code snippet: -
CSV import def Connection (): for = open ("sample.txt", "R") for data =. Line = line.strip ("\ n") line = line.split ("headline"); line = line.strip (" , ") Time = line [0] rows = [time for x]. Full ('in') if x] i = lane (rows) if i == 0: other release: hours, minutes, seconds = line [ 0], lines [1], lines [2] minute = int (minutes) if minutes & gt; = 0 more minute & lt; 15: print hours, minute print line [1:] alif min & gt; = 15 more minutes & lt; 30: print hours, minute print line [1:] alif min & gt; = 30 more minutes & lt; 45: print hours, minute print line [1:] alif min & gt; = 45 more minutes & lt; 59: Print Hours, Minutes Print Line [1:] Connection () [1:] Returns the right data for each interval and I am writing to write and write the CSV files I am information So instead of printing [1:], I want it to be written in the CSV file of that interval, which are written with the appropriate naming conventions as described in the above description.
Expected Output: -
2014-07-24 15: 00 CSV should contain 1,1,1,1,1001 1,1,1,1 , 1001,1,1,1,1001 2014-07-24 15:15 in CSV 1,1,1,1,1001,1,1,1,1,1,1,1,1,1,1,1001 and so on should be 15.30 for .csv and 15.45 csv. Keeping in mind this is a small part of the data. The actual data is meant for every hour of data to create 4 CSV files for 24 * 4 files each day for each day. So how can I make my code stronger and more efficient?
Any help? Thanks
, seems like a job for itertools.groupby The time stamp is increasingly increasing the price: from itertools as date time datetime from date import code> Import imap, from GroupBy operator import get_first = itemgetter (0) get_second = itemgetter ( 1) DEF (line) process_line itemgetter: timestamp_string, _, value = line.partition (',') timestamp = DateTime.strptime (timestamp_string, '% Y-% m-% D% H:% M:% S') Returns (timestamp.rele (min = timestamp.minet // 15 * 15, sec = 0), value) DE Main (): With Open ('sample.txt', 'R' as lines): Date, in group GroupBy (IMAP (process_line, lines), get_first): Open ('{0:% Y --comp.format (date), 'mou') with out_file as: out_file.writelines (IMAP (get_second, group)) if __name__ == '__main__': -% m-% d% H_% M} Main ()
Comments
Post a Comment