title: Generating multiple CSV files on hourly basis in Python
body: <p>I have a python module called HourlyCsvGeneration.py. I have some data which is being generated on hourly basis which is is sample.txt. Here is the sample of the data in the sample.txt:-</p>
  
  <pre><code>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,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:00,1,1,1,1,1001
  2014-07-24 15:30:00,1,1,1,1,1001
  2014-07-24 15:31:00,1,1,1,1,1001
  2014-07-24 15:32:00,1,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:00,1,1,1,1,1001
  </code></pre>
  
  <p>As you can see there are 4 intervals 00-15, 15-30, 30,45 and 45-59 and the next hour starts and so on. I am writing the code that would read the data in this txt file and generating 4 CSV files for every hour in a day. So analysing the above data the 4 CSV files should be generated should have naming convention like 2014-07-24 15:00.csv containing the data between 15:00 and 15:15, 2014-07-24 15:15.csv containing the data between 15:15 and 15:30 and so on for every hour. The python code must handle all this. </p>
  
  <p>Here is my current code snippet:-</p>
  
  <pre><code>import csv
  def connection():
      fo = open("sample.txt", "r")
      data = fo.readlines()
      header = ['tech', 'band', 'region', 'market', 'code']
      for line in data:
              line = line.strip("\n")
              line = line.split(",")
              time = line[0]
              lines = [x for x in time.split(':') if x]
              i = len(lines)
              if i == 0:
                      continue
              else:
                      hour, minute, sec = lines[0], lines[1], lines[2]
                      minute = int(minute)
                      if minute &gt;= 0 and minute &lt; 15:
                              print hour, minute
                              print line[1:]
                      elif minute &gt;= 15 and minute &lt; 30:
                              print hour, minute
                              print line[1:]
                      elif minute &gt;= 30 and minute &lt; 45:
                              print hour, minute
                              print line[1:]
                      elif minute &gt;=45 and minute &lt; 59:
                              print hour, minute
                              print line[1:]
  connection()
  </code></pre>
  
  <p>[1:] gives the right data for each interval and I am kind off stuck in generating CSV files and writing the data. So instead of printing [1:], I want this to be get written in the csv file of that interval with the appropriate naming convention as explained in the above description.</p>
  
  <p>Expected output:-</p>
  
  <pre><code>2014-07-24 15:00.csv must contain
  1,1,1,1,1001
  1,1,1,1,1001
  1,1,1,1,1001
  
  2014-07-24 15:15.csv must contain
  1,1,1,1,1001
  1,1,1,1,1001
  1,1,1,1,1001
  </code></pre>
  
  <p>and so on for 15.30.csv and 15.45.csv. Keeping in mind this is just a small chunk of data. The actual data is for every hour of the data. Meaning generating 4 csv files for each hour that is 24*4 files for one day. So how can I make my code more robust and efficient?</p>
  
  <p>Any help?Thanks</p>