2016-06-03 10 views
1

Ich habe eine Datei mit Daten und ich möchte nach der maximalen Lesung pro Stunde suchen.Python verschachtelte Schleife arbeitet mit Datendatei

Okay, also habe ich meinen Code aktualisiert, aber es bleibt nur in einer Stunde.

def maximum(): 
source = open ('dataV.csv', 'r') 
result = open ('dataV-max.csv', 'w') 
line = source.readline() 

line = source.readline() 

max_hour = line[23:] 
hour = line[12:14] 

while line != '': 
    hour = line[12:14] 
    line = source.readline() 
    if hour == line[12:14]: 
     if line[23:] > max_hour: 
      max_hour = line[23:] 
     result.write(line) 

source.close() 
result.close() 

Ich habe ein Problem mit der verschachtelten Schleife, denke ich. Ich verstehe nicht, wie man es durch die ganze Datei gehen lässt.

Hier ist ein Teil der Datei:

'time PST', saturn03.820.A.AlgaeWatch [microg/l] 
'2014-04-25 00:04:48',3.35 
'2014-04-25 00:04:54',3.225 
'2014-04-25 00:05:00',3.15 
'2014-04-25 00:07:48',3.4 
'2014-04-25 00:07:54',3.4 
'2014-04-25 00:08:00',3.375 
'2014-04-25 00:10:48',3.45 
'2014-04-25 00:10:54',3.325 
'2014-04-25 00:11:00',3.425 
'2014-04-25 00:13:49',3.45 
'2014-04-25 00:13:54',3.5 
'2014-04-25 00:14:00',3.525 
'2014-04-25 00:16:48',3.725 
+0

Bitte klären Sie die Frage ein wenig. Was wird der Inhalt der Datei sein? Beispieldateiinhalt ist in Ordnung, wenn Sie geben. Bitte geben Sie einen Beispielinhalt und was Sie von diesem Beispielinhalt erwarten. – sagar

+0

@sagar ok Ich habe es getan – in43sh

+0

Vielen Dank für den Inhalt der Eingabe. Bitte lassen Sie mich wissen, was die Ausgabe ist, die Sie erwarten. – sagar

Antwort

0

den Eingang Gegeben:

'time PST', saturn03.820.A.AlgaeWatch [microg/l] 
'2014-04-25 00:04:48',3.35 
'2014-04-25 00:04:54',3.225 
'2014-04-25 00:05:00',3.15 
'2014-04-25 00:07:48',3.4 
'2014-04-25 00:07:54',3.4 
'2014-04-25 00:08:00',3.375 
'2014-04-25 00:10:48',3.45 
'2014-04-25 00:10:54',3.325 
'2014-04-25 00:11:00',3.425 
'2014-04-25 00:13:49',3.45 
'2014-04-25 00:13:54',3.5 
'2014-04-25 01:14:00',3.525 
'2014-04-25 02:16:48',3.725 

Das Programm:

#! /usr/bin/env python 
"""Usually a ready made file parser like csv module or even panda 
et al. for more complete service is the way to go here but one may 
want to know how to basically iterate and parse a little one self. 
This is also for the date time parsing which one typically also 
delegates to datetime module or the like.""" 
from __future__ import print_function 
import sys 


def hourly_maxima(in_file, out_file): 
    """Extract calendar hourly maximum readings from in_file, 
    write to out_file. If files do not exist or are 
    not accessible exceptions will happily raise ;-). 
    Input is expected to be ordered ascending by time 
    stamp.""" 

    field_sep = ',' 
    with open(in_file, 'rt') as f_i, open(
      out_file, 'wt') as f_o: # May raise here 
     f_i.readline() # Ignore header, be optimistic 

     ts_raw = None 
     hourly_maximum = None 
     current_hour = None # Group by calendar hour stored in tuples 
     date_sep = '-' 
     # Expect sample data line to document flow: 
     # '2014-04-25 00:04:48',3.35 
     for line in f_i.readlines(): # Digest rest of lines 
      if not line: 
       break # stop on first empty line 
      ts, reading = line.strip().split(field_sep) # May raise ... 
      r_float = float(reading) # May raise ... 

      # Map timestamp ts to calendar hour 
      ts_raw = ts.strip("'") 
      year, month, day = ts_raw[:10].split(date_sep) 
      hour = ts_raw[11:13] 
      cand_hour = (year, month, day, hour) 
      if current_hour is None: 
       current_hour = cand_hour 

      if cand_hour == current_hour: # We seek the maximum 
       if hourly_maximum is None or r_float > hourly_maximum: 
        hourly_maximum = r_float 
      else: # report hourly maximum of previous hour and reset 
       print(ts_raw, hourly_maximum) # Also report matching hour? 
       f_o.write('%s\n' % (str(hourly_maximum))) 
       current_hour = cand_hour 
       hourly_maximum = r_float 

     # Flush the last result kept in hourly_maximum: 
     print(ts_raw, hourly_maximum) # Also report matching hour? 
     f_o.write('%s\n' % (str(hourly_maximum))) 


def main(): 
    """Drive the extraction.""" 
    in_file = 'dataV.csv' if len(sys.argv) < 2 else sys.argv[1] 
    out_file = 'dataV-max.csv' if len(sys.argv) < 3 else sys.argv[2] 

    hourly_maxima(in_file, out_file) 

if __name__ == '__main__': 
    sys.exit(main()) 

Ausbeuten:

2014-04-25 01:14:00 3.5 
2014-04-25 02:16:48 3.525 
2014-04-25 02:16:48 3.725 

auf std Ausgabe und in der Datei:

3.5 
3.525 
3.725 

Jetzt ist das was du wolltest? Ich denke schon. Viel Raum für Verbesserungen, Härte und zusätzliche Eleganz.

Mit dem Lernen von Python weitermachen.

PS: Sorry war eine Weile offline.

0

Held Sie gehen:

import sys 
def fileParser(sourcefileName, destinationfileName): 
    fd = open(sourcefileName) 
    lines = fd.readlines() 
    hourMaxDict = dict() 
    for line in lines[1:]: 
      hour = line.split(" ")[1].split(":")[0] 
      maxRead = float(line.split(",")[-1].rstrip()) 
      if hour in hourMaxDict.keys() and hourMaxDict[hour] > maxRead: 
        continue 
      else: 
        hourMaxDict[hour] = maxRead 
    destFd = open(destinationfileName, "a") 
    for key, val in hourMaxDict.iteritems(): 
      val = str(val) + "\n" 
      destFd.write(str(val)) 
    fd.close() 
    destFd.close() 

if __name__ == "__main__": 
    fileParser(sys.argv[1], sys.argv[2]) 

Ausführung:

[email protected]:~$ python fileReader.py sourceFile.txt destinationFile.txt 

Eingabedatei Inhalt:

'time PST', saturn03.820.A.AlgaeWatch [microg/l] 
'2014-04-25 00:04:48',3.35 
'2014-04-25 00:04:54',3.225 
'2014-04-25 00:04:48',3.35 
'2014-04-25 00:04:54',3.225 
'2014-04-25 00:05:00',3.15 
'2014-04-25 00:07:48',3.4 
'2014-04-25 00:07:54',3.4 
'2014-04-25 00:08:00',3.375 
'2014-04-25 00:10:48',3.45 
'2014-04-25 00:10:54',3.325 
'2014-04-25 00:11:00',3.425 
'2014-04-25 00:13:49',3.45 
'2014-04-25 00:13:54',3.5 
'2014-04-25 01:14:00',3.525 
'2014-04-25 01:16:48',3.725 

Ausgabedatei Inhalt:

3.5 
3.725 

können Sie Split intensiv nutzen, das Gleiche zu erreichen. Hoffe das wird helfen. :-)

Verwandte Themen