2016-08-08 25 views
0

Ich habe folgenden Fall: Ich brauche die Zeit eines Features in einer CSV-Datei und vergleichen Sie es mit der Zeit der Bilder von jemandem gemacht. Dann muss ich 2 (oder weniger) Übereinstimmungen finden. Ich werde die ersten beiden Bilder, die ich finde, in einem 2-Minuten-Intervall von der Zeit des Features bis zu dieser Funktion zuordnen. Ich schaffte es, zwei Wörterbücher mit den Details zu erstellen: feature_hours enthält id und Zeit des Features. photo_hours enthält photo_path und Uhrzeit des Fotos. sorted_feature und sorted_photo sind zwei Listen, die die zwei Wörterbücher sortiert haben. Das Problem ist, dass in der Ausgabe csv-Datei habe ich nur 84 Zeilen abgeschlossen und einige sind leer. Die Feature-CSV-Datei verfügt über 199 Funktionen. Ich denke ich habe j zu oft erhöht. Aber ich brauche einen klaren Blick von einem Profi, weil ich es nicht herausfinden kann. Hier ist der Code:Export nach CSV in Python


j=1 
sheet1.write(0,71,"id") 
sheet1.write(0,72,"feature_time") 
sheet1.write(0,73,"Picture1") 
sheet1.write(0,74,"Picture_time") 
sheet1.write(0,75,"Picture2") 
sheet1.write(0,76,"Picture_time") 
def write_first_picture(): 

    sheet1.write(j,71,feature_time[0]) 
    sheet1.write(j,72,feature_time[1]) 
    sheet1.write(j,73,photo_time[0]) 
    sheet1.write(j,74,photo_time[1]) 

def write_second_picture(): 

    sheet1.write(j-1,75,photo_time[0]) 
    sheet1.write(j-1,76,photo_time[1]) 

def write_pictures(): 

    if i==1: 

     write_first_picture() 
    elif i==2: 
     write_second_picture() 

for feature_time in sorted_features: 
    i=0 
    for photo_time in sorted_photo: 
     if i<2: 
      if feature_time[1][0]==photo_time[1][0]: 
       if feature_time[1][1]==photo_time[1][1]: 
        if feature_time[1][2]<photo_time[1][2]: 
         i=i+1 
         write_pictures() 
         j=j+1 
        elif int(feature_time[1][1])+1==photo_time[1][1]: 
         i=i+1 
         write_pictures() 
         j=j+1 
        elif int(feature_time[1][1])+2==photo_time[1][1]: 
         i=i+1 
         write_pictures() 
         j=j+1 
        elif int(feature_time[1][0])+1==photo_time[1][0]: 
         if feature_time[1][1]>=58: 
          if photo_time[1][1]<=02: 
           i = i+1 
           write_pictures() 
           j=j+1 

Edit: Hier ist Beispiele der beiden Listen: Liste mit Funktionen: [('-70', ('10', '27', '03')), ('-73', ('10', '29', '50'))] Fotoliste: [('20160801_125133-1151969393.jpg', ('12', '52', '04')), ('20160801_125211342753906.jpg', ('12', '52', '16'))]

Antwort

0

Es gibt ein CSV-Modul für Python, mit dem Sie diese Dateien laden können. Sie könnten die Ergebnisse sortieren, um zu versuchen, effizienter zu sein/Ihre Schecks auch kurzzuschließen. Ich kann nicht wirklich sagen, was die i und j Variablen darstellen sollen, aber ich bin ziemlich sicher, dass Sie so etwas wie folgendes tun:

import csv 

def hmstoseconds(hhmmss): 
    # 60 * 60 seconds in an hour, 60 seconds in a min, 1 second in a second 
    return sum(x*y for x, y in zip(hhmmss, (60*60, 60, 1))) 

features = [] 
# features model looks like tuple(ID, (HH, MM, SS)) 
with open("features.csv") as f: 
    reader = csv.reader(f) 
    features = list(reader) 

photos = [] 
# photos model looks like tuple(filename, (HH, MM, SS)) 
with open("photos.csv) as f: 
    reader = csv.reader(f) 
    photos = list(reader) 

for feature in features: 
    for photo in photos: 
     # convert HH, MM, SS to seconds and find within 2 min (60s * 2) 
     # .. todo:: instead of nested for loops, we could use filter() 
     if abs(hmstoseconds((feature[1]) - hmstoseconds(photo[1])) <=(60 * 2): 
      # the photo was taken within 2 min of the feature 
      <here, write a photo> 

Um dies besser verwaltbar/lesbar zu machen, können Sie auch namedtuples nutzen könnten um die Datenmodelle besser darzustellen:

import csv 
from collections import namedtumple 

# model definitions to help with readability/maintainence 
# if the order of the indices changes or we add more fields, we just need to 
# change them directly here instead of tracking the indexes everywhere 
Feature = namedtuple("feature", "id, date") 
Photo = namedtuple("photo", "file, date") 

def hmstoseconds(hhmmss): 
    # 60 * 60 seconds in an hour, 60 seconds in a min, 1 second in a second 
    return sum(x*y for x, y in zip(hhmmss, (60*60, 60, 1))) 

def within_two_min(date1, date2): 
    # convert HH, MM, SS to seconds for both dates 
    # return whether the absolute difference between them is within 2 min (60s * 2) 
    return abs(hmstoseconds(date1) - hmstoseconds(date2)) <= 60 * 2 

if __name__ == '__main__': 
    # using main here means we avoid any nasty global variables 
    # and only execute this code when this file is run directly 
    features = [] 
    with open("features.csv") as f: 
     reader = csv.reader(f) 
     features = [Feature(f) for f in reader] 

    photos = [] 
    with open("photos.csv) as f: 
     reader = csv.reader(f) 
     photos = [Photo(p) for p in reader] 

    for feature in features: 
     for photo in photos: 
      # .. todo:: instead of nested for loops, we could use filter() 
      if within_two_min(feature.date, photo.date): 
       <here, write a photo> 

Hoffentlich bringt Sie das in die richtige Richtung. Ich verstehe nicht ganz, was Sie mit i und j und dem ersten/zweiten "write_picture" Zeug machen wollten, aber ich hoffe, dass Sie den Umfang und den Zugriff in Python besser verstehen.