Home > Mobile >  Python strip date from CSV
Python strip date from CSV

Time:11-13

I've currently got a loop that iterates through each line of a CSV and pulls out information and to enter a website via selenium.

I'm currently struggling with a dropdown box date field. The date is format 30/12/2000.

I think I should be able to strip out each part of the date and send it to send keys in that box and press enter.

I'm using something like this.

from datetime import datetime

with open('/Users/me/Downloads/dates_test.csv',newline='') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        try:
           driver = find stuff on page
           d = (row[1])
           objd = datetime.strptime(d, '%d %m %Y')
           sign_up_date = driver.find_element_by_xpath("//*[@id='membership-signed-up-     on']/a").click()
            sleep(1)
            day1 = driver.find_element_by_xpath("//*[@id='membership_signed_up_on_3i']").click()
            day1.send_keys(objDate.day)
            day1.send_keys(Keys.RETURN)

Two questions,

  1. I'm currently getting this error.

    ValueError: time data '17/05/2021' does not match format '%d %m %Y'

  2. Am I on the right track. Is there a better way to do this.

CodePudding user response:

You're very close. Just change this line:

objd = datetime.strptime(d, '%d %m %Y')

to this

objd = datetime.strptime(d, '%d/%m/%Y')

CodePudding user response:

Alternatively, if you do not want to deal with date formats, you could use the dateutil library:

from dateutil import parser as p

objd = p.parse(d)
  • Related