I have a function from Vasilis G.'s answer here :
from datetime import datetime def getTotal(line): dates = list(datetime.strptime(elem, '%Y-%m-%d') for elem in line.split(', ')) total = 0 for i in range(0, len(dates), 2): delta = dates[i+1] - dates[i] if delta.days > 90: total += 2 else: total += 1 return total
I then used:
with open('dataset.csv','r') as csvfile: # use with to auto-close file
for row in csvfile.readlines(): # pin_id
getTotal(row) # board_id and section can use defaults
time.sleep(random.randint(1,3)) # wait 1-3 seconds
And I would like to apply it on every line in my csv file but it's giving me "ValueError : unconverted data remains : 2022-06-03,2021-05-29,2024-06-03,2022-05-29,2026-06-03,2023-05-29,2028-06-03,2024-05-29,2030-06-03,2025-05-29,2032-06-03
which coincide with the first line of my .csv file.
FINAL :
from datetime import datetime
def getTotal(line):
dates = [datetime.strptime(elem.strip(), '%Y-%m-%d') for elem in line.split(',')]
total = 0
for i in range(0, len(dates), 2):
delta = dates[i+1] - dates[i]
if delta.days > 90:
total += 2
else:
total += 1
return total
print(getTotal(dates))
with open('dataset.csv','r') as csvfile:
for row in csvfile.readlines():
getTotal(row)
Try this:
def getTotal(line):
dates = [datetime.strptime(elem.strip(), '%Y-%m-%d') for elem in line.split(',')]
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.