python编辑csv
import csv
import time
def timestamp_to_timestr(timeStamp):
timeArray = time.localtime(int(timeStamp))
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
return otherStyleTime
f=open('C:/look.csv','r')
reader = csv.DictReader(f)
headers = reader.fieldnames
look = list(reader)
f=open('C://up.csv','r')
up= list(csv.DictReader(f))
f.close()
for l in up:
l['start_time'] = timestamp_to_timestr(l['start_time'])
l['end_time'] = timestamp_to_timestr(l['end_time'])
for r in look :
for s in up:
if r['look_id'] == s['look_id'] and r['last_update_time'] >= s['start_time'] and r['last_update_time'] < s['end_time']:
r['up_id'] = s['up_id']
break
with open('C:/lookup.csv','w') as f:
f_csv = csv.DictWriter(f, headers, lineterminator='\n')
f_csv.writeheader()
f_csv.writerows(look)
作者:catmelo
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。