export to csv in python -


i have following case: need time of feature in csv file , compare time of pictures taken someone. need find 2 (or less) matches. assign first 2 pictures find in 2 mins interval time of feature feature. managed create 2 dictionaries details: feature_hours contains id , time of feature. photo_hours contains photo_path , time of photo. sorted_feature , sorted_photo 2 lists sorted 2 dictionaries. problem in output csv file have 84 rows completed , blank. feature csv file has 199 features. think incremented j often. need clear pro, because cannot figure out. here code:


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()  feature_time in sorted_features:     i=0     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+1                                 write_pictures()                                  j=j+1 

edit: here examples of 2 lists: features list: [('-70', ('10', '27', '03')), ('-73', ('10', '29', '50'))] photo list: [('20160801_125133-1151969393.jpg', ('12', '52', '04')), ('20160801_125211342753906.jpg', ('12', '52', '16'))]

there csv module python load these files. sort results try more efficient/short-circuit checks well. cannot tell , j variables meant represent, pretty sure can following:

import csv  def hmstoseconds(hhmmss):     # 60 * 60 seconds in hour, 60 seconds in min, 1 second in second     return sum(x*y x, y in zip(hhmmss, (60*60, 60, 1)))  features = [] # features model looks tuple(id, (hh, mm, ss)) open("features.csv") f:     reader = csv.reader(f)     features = list(reader)  photos = [] # photos model looks tuple(filename, (hh, mm, ss)) open("photos.csv) f:     reader = csv.reader(f)     photos = list(reader)  feature in features:     photo in photos:         # convert hh, mm, ss seconds , find within 2 min (60s * 2)         # .. todo:: instead of nested loops, use filter()         if abs(hmstoseconds((feature[1]) - hmstoseconds(photo[1])) <=(60 * 2):             # photo taken within 2 min of feature             <here, write photo> 

in order make more maintainable/readable, use namedtuples better represent data models:

import csv collections import namedtumple  # model definitions readability/maintainence # if order of indices changes or add more fields, need  # change them directly here instead of tracking indexes everywhere feature = namedtuple("feature", "id, date") photo = namedtuple("photo", "file, date")  def hmstoseconds(hhmmss):     # 60 * 60 seconds in hour, 60 seconds in min, 1 second in second     return sum(x*y x, y in zip(hhmmss, (60*60, 60, 1)))  def within_two_min(date1, date2):     # convert hh, mm, ss seconds both dates     # return whether absolute difference between them within 2 min (60s * 2)     return abs(hmstoseconds(date1) - hmstoseconds(date2)) <= 60 * 2   if __name__ == '__main__':     # using main here means avoid nasty global variables     # , execute code when file run directly     features = []     open("features.csv") f:         reader = csv.reader(f)         features = [feature(f) f in reader]      photos = []     open("photos.csv) f:         reader = csv.reader(f)         photos = [photo(p) p in reader]      feature in features:         photo in photos:             # .. todo:: instead of nested loops, use filter()             if within_two_min(feature.date, photo.date):                 <here, write photo> 

hopefully gets moving in right direction. don't understand trying , j , first/second "write_picture" stuff, hoping understand better scope , access in python.


Comments