2016-08-08 9 views
0

私は次のような場合があります: csvファイルの機能の時間を取得し、それを誰かが撮影した写真の時間と比較する必要があります。 それから私は2つ(またはそれ以下)のマッチを見つける必要があります。私は、フィーチャーの時間からそのフィーチャーまでの2分の間隔で見つけた最初の2枚の絵を割り当てます。 詳細を含む2つの辞書を作成できました。feature_hoursにはidと時間が含まれています。 photo_hoursにはphoto_pathと写真の時間が含まれています。 sorted_featureおよびsorted_photoは、2つの辞書をソートした2つのリストです。 問題は、出力csvファイルでは、私は84行しか完成せず、いくつかは空白であることです。機能csvファイルには199の機能があります。私はjをあまりにも頻繁に増やしたと思う。しかし、私はそれを把握することができないので、私はプロから明確な外観が必要です。ここ は、コードは次のとおりです。Pythonでcsvにエクスポート


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 

編集: 特長一覧::[( '-70'、( '10'、 '27'、 '03')ここでは二つのリストの例があります)、( '-70'、('10 '、' 29 '、'50'))] 写真リスト:[( '20160801_125133-1151969393.jpg'、( '12'、 '52'、 '04') )、( '20160801_125211342753906.jpg'、('12 '、' 52 '、' 16 '))]

答えて

0

これらのファイルを読み込むためのPython用のCSVモジュールがあります。より効率的にしようとするために結果を並べ替えることも、チェックを短絡することもできます。あなたもnamedtuplesを使用することができ、リード/これはより保守にするために

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> 

:私は実際にiとj変数が表すように意図されているものを言うことはできませんが、私はあなたが以下のような何かを行うことができますかなり確信しています

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> 

これが正しい方向に動くことを望みます。私はあなたがiとjと最初と2番目の "write_picture"のもので何をしようとしているのかを完全には理解していませんが、Pythonのスコープとアクセスをより良く理解したいと思っています。

関連する問題