for
l oop в строке 46 не работает. Переменная я остаюсь в len
списка. Я вставил весь свой код и образцы
import csv
from openpyxl.utils.datetime import from_excel, to_ISO8601
import datetime
import toolbox
import timeit
tic = timeit.default_timer()
for x in range(1,2): # here the code will read from 34 csv files containing GPS informations into a list of lists
csvfilepath = "C:\\Users\\pavlo\\PycharmProjects\\PEXproject1\\DataCleaning\\sample data\\GpsData{}.csv".format(x)
# Here the gps data is loaded into list of lists gpsdata and the timestamp is converted to ISO8601
with open(csvfilepath, "r") as f:
reader = csv.reader(f)
headers = next(reader)
gpsdata = list(list(rec) for rec in csv.reader(f, delimiter=','))
for row in gpsdata:
GpsTimestamp = datetime.datetime.strptime(row[10], '%m/%d/%Y %H:%M')
row[10] = to_ISO8601(dt=GpsTimestamp)
driving = 0
idle = 0
working = 0
last_ON = ""
dists = [[], [], [], []] #this list of lists will capture the distances in the various states
ytfilepath = "C:\\Users\\pavlo\\PycharmProjects\\PEXproject1\\DataCleaning\\sample data\\VehicleEvents.csv"
# Capturing the headers in vehicle list
with open(ytfilepath, "r") as f:
reader = csv.reader(f)
headers = next(reader)
vehiclist = csv.DictReader(open(ytfilepath, "r"))
for row in vehiclist:
JobID = row["ID"]
vehicle_no = row['Vhcl']
mode = row['Mode']
ON = to_ISO8601(dt=from_excel(value=float(row["Engineon"])))
OFF = to_ISO8601(dt=from_excel(value=float(row["Engineoff"])))
try:
ST = to_ISO8601(dt=from_excel(value=float(row["WorkStart"])))
ED = to_ISO8601(dt=from_excel(value=float(row["WorkEnd"])))
except:
ST = 2000
ED = 2000
PS = to_ISO8601(dt=from_excel(value=float(row["ParkStart"])))
PE = to_ISO8601(dt=from_excel(value=float(row["ParkEnd"])))
driving = idle = working = 0
for i in range(len(gpsdata)): #I go through all rows of gps list
if gpsdata[i][10] <= ON or gpsdata[i][10] >= OFF: #I want to exclude if the vehicle was off at the gps timestamp
if vehicle_no == gpsdata[0][2]:
c1 = gpsdata[i][10]
c2 = gpsdata[i][8]
if mode == 'Manual' :
if PS <= c1 <= PE:
driving += c2
if ON <= c1 <= PS:
idle += c2
else:
if PE <= c1 <= OFF :
driving += c2
if ON <= c1 <= PE:
idle += c2
if OFF <= c1 <= last_ON :
working += c2
toc = timeit.default_timer()
print(i)
if i % 100 == 0:
print('processing algorithm: {}'.format(toc - tic))
print('we are at row {}'.format(i))
dists[0].append(JobID)
dists[1].append(driving)
dists[2].append(idle)
dists[3].append(working)
# print(dists)
driving = idle = working = 0
toc = timeit.default_timer()
with open("outfile{}.csv".format(x), 'w') as outfile:
outfile_writer = csv.writer(outfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
outfile_writer.writerow((dists[0], dists[1], dists[2], dists[3])),
tac = timeit.default_timer()
print('exporting {}'.format(tac - toc))
GpsData1 =
AA,ddate,gps_id,latitude,longitude,speed,accuracy,bearing,distance,ip,Timestamp
6631060599,20191216,V001,50.94269749,71.30314074,0.61,16.687,120.2,4564.41,,12/16/2019 0:00
6631065030,20191216,V001,50.9437751,71.30016186,7.46,33.374,0,48356.2,,12/16/2019 4:31
6631065153,20191216,V001,50.94291191,71.30116373,4.04,24.272,31.6,12747.4,,12/16/2019 19:07
6631060271,20191217,V001,50.94866914,71.30378451,4.49,4.551,0,8368.56,,12/17/2019 17:10
6631060630,20191218,V001,50.94821482,71.30276768,5.16,50.061,27.7,28869.4,,12/18/2019 4:54
6631069096,20191219,V001,50.94534268,71.30259522,2.11,24.272,63.1,9506.68,,12/19/2019 2:55
6631059877,20191219,V001,50.94332513,71.30145316,1.72,10.619,32.4,14834.6,,12/19/2019 17:13
6631068956,20191219,V001,50.94716712,71.30282306,3.14,21.238,300.4,11038.9,,12/19/2019 21:33
6631067712,20191220,V002,50.93724855,71.29651212,9.71,18.204,206,96770.7,,12/20/2019 16:22
6631065674,20191221,V002,50.9467955,71.30202786,6.96,15.008,31.1,28784.4,,12/21/2019 0:07
6631061274,20191221,V002,50.94207924,71.29937677,7.86,30.34,194.3,43064.3,,12/21/2019 14:15
6631063673,20191221,V002,50.94591501,71.30136721,0.83,22.755,293,27036.3,,12/21/2019 18:37
6631064438,20191221,V002,50.94743564,71.30240991,0.56,39.442,259.7,3525.12,,12/21/2019 22:25
6631060673,20191222,V002,50.94580669,71.30148016,5.55,13.653,204.3,53280,,12/22/2019 3:26
6631068423,20191222,V002,50.91783065,71.28983391,6.24,28.823,2.5,42947.8,,12/22/2019 4:14
6631066879,20191222,V002,50.94432872,71.30065772,6.25,19.721,209.8,36728.9,,12/22/2019 11:44
6631068174,20191223,V002,50.93346341,71.29449935,9.26,25.789,28.8,42795.9,,12/23/2019 4:02
VehicleEvents =
ID,Vhcl,Mode,CID,Engineon,Engineoff,WorkStart,WorkEnd,ParkStart,ParkEnd
85807835,V001,Manual,AAA5846129341,43815.08135,43815.08938,,,43815.07334,43815.08211
85809668,V002,Auto,AAA8022407504,43815.08938,43815.10535,43815.08938,43815.10535,43815.08938,43815.09535
85810976,V003,Auto,AAA0571518764,43815.10538,43815.11505,43815.10538,43815.11505,43815.10959,43815.11505
85813025,V004,Manual,AAA3189634914,43815.11506,43815.12703,43815.11506,43815.12703,43815.11506,43815.12303
85813028,V005,Manual,AAA1940741282,43815.11506,43815.12703,,,43815.11506,43815.12372
85815305,V006,Manual,AAA1894455904,43815.12705,43815.14505,43815.12705,43815.14692,43815.13366,43815.14505
85815467,V001,Auto,AAA9538532026,43815.12705,43815.14692,,,43815.13361,43815.14692
85821410,V002,Auto,AAA8391952906,43815.14696,43815.20984,43815.14696,43815.20984,43815.14696,43815.15206
85873358,V003,Manual,AAA4922964611,43815.72992,43815.74645,43815.72992,43815.74645,43815.73586,43815.74645
85875020,V004,Manual,AAA6039158858,43815.74646,43815.76461,43815.74646,43815.76601,43815.75975,43815.76461
85875137,V005,Manual,AAA7495366053,43815.74646,43815.76601,,,43815.75972,43815.76601
85877825,V006,Auto,AAA7638509608,43815.76602,43815.79272,43815.76602,43815.79429,43815.76602,43815.77079
85877942,V001,Auto,AAA1265572219,43815.76602,43815.79429,,,43815.76602,43815.76985
85879040,V002,Manual,AAA2968711840,43815.79431,43815.80431,43815.79431,43815.80431,43815.79431,43815.80127
85882028,V003,Manual,AAA7692514875,43815.80432,43815.83537,43815.80432,43815.83537,43815.80432,43815.82109
85884230,V004,Manual,AAA4674654439,43815.83538,43815.85745,43815.83538,43815.85745,43815.83538,43815.84685
85885460,V005,Auto,AAA8107186366,43815.85748,43815.86895,43815.85748,43815.86895,43815.86016,43815.86895
85885994,V006,Auto,AAA5796012701,43815.86916,43815.875,43815.86916,43815.875,43815.87164,43815.875
85886981,V001,Manual,AAA8719492664,43815.87502,43815.88547,43815.87502,43815.88547,43815.8795,43815.88547
85890116,V002,Manual,AAA2865936367,43815.88549,43815.91355,43815.88549,43815.91355,43815.89722,43815.9073
85890119,V003,Manual,AAA1887612592,43815.88549,43815.91355,,,43815.88549,43815.8972
85891310,V004,Auto,AAA1144467605,43815.91358,43815.92514,43815.91358,43815.92514,43815.91856,43815.92514
85892144,V005,Auto,AAA3719694551,43815.92516,43815.93397,43815.92516,43815.93523,43815.92922,43815.93397