5

I tried this code to convert string of dates into another format but i have number of strings which is not taken by this code.

import dateparser
dateparser.parse(str1[0], date_formats=['%d %B %Y'] )

Can anyone help me?

5
  • 1
    So what format are the dates that you're having trouble with in? Commented Apr 15, 2018 at 9:51
  • Im having dates in for example 30 Jan 2010 , I want it in 2010-01-30 Commented Apr 15, 2018 at 9:59
  • If you have questions provide your code as mvce. If you encounter errors, copy and paste the error message verbatim ( word for word) into your question. Post the problematic part of your data-samples. Edit your question to add the needed information so we can easily help you. Commented Apr 15, 2018 at 10:16
  • I tried import datefinder string_with_dates = """ entries are due by January 4th, 2017 at 8:00pm created 01/15/2005 by ACME Inc. and associates. """ matches = datefinder.find_dates(string_with_dates) for match in matches: print match it prints everything well but how I stored this output into list Commented Apr 15, 2018 at 10:31
  • I used s=[] , s.append(match) but I don't want date time text to be added in it. Commented Apr 15, 2018 at 10:38

2 Answers 2

3

You can convert your date format using datetime, which is available in the standard library:

from datetime import datetime

x = '30 Jan 2010'
res_single = datetime.strptime(x, '%d %b %Y').strftime('%Y-%m-%d')
# '2010-01-30'

lst = ['30 Jan 2015', '10 Dec 2010', '20 Apr 2020', '15 Feb 2005']
res_lst = [datetime.strptime(x, '%d %b %Y').strftime('%Y-%m-%d') for x in lst]
# ['2015-01-30', '2010-12-10', '2020-04-20', '2005-02-15']
Sign up to request clarification or add additional context in comments.

Comments

0

You can use datetime and a straightforward for loop. Preparations:

from datetime import datetime #import
dates = ['20 Feb 2010', '21 Mar 2012', '22 Apr 2014', '24 May 2016'] #sample dates
parsed_dates = [] #initialise empty list to store the parsed dates in

Actual loop:

for date in dates:
    d_parsed = datetime.strptime(date, '%d %b %Y').strftime('%Y-%m-%d') #transform date
    parsed_dates.append(d_parsed) #add transformed date to new list

This results in

print(parsed_dates)
#>>> ['2010-02-20', '2012-03-21', '2014-04-22', '2016-05-24']

If your dates are hidden but always in the same way - e.g. always preceded by a string of different lengths but situated in the end, as your example in the comments - use this code:

from datetime import datetime #import
hidden_dates = ['For example16 Apr 2018','another example04 Apr 2018', '20 Feb 2010', 'testdtas_21 Mar 2012', 'some characters24 22 Apr 2014', 'words 24 May 2016'] #sample dates
parsed_dates = [] #initialise empty list to store the parsed dates in

for hidden_d in hidden_dates:
    date = hidden_d[-11:]
    d_parsed = datetime.strptime(date, '%d %b %Y').strftime('%Y-%m-%d') #transform date
    parsed_dates.append(d_parsed) #add transformed date to new list

print(parsed_dates)
#>>> ['2010-02-20', '2012-03-21', '2014-04-22', '2016-05-24']

3 Comments

What how will I separate dates from multiple strings in list without using datefinder and then stored the data into list to format the date?
I don't understand. Can you explain what you mean, and post the code in the question? :)
I have a string like ['For exampleApr 16, 2018','another exampleApr04, 2018'.........] I am able to get dates with date finder but these method dates are not in same order as in sentence, my question is is there any other method from which I can separate the dates from sentence like [Apr16, 2018, 'Apr 04, 2018'....]as in question I have asked for single string I can extract it but for num.ber of strings loop was not able to work, I hope its clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.