1

Consider the following example str of dates:

'20180101,20180102,20180103,20180104,20180105,20180106,20180107,20180108,20180109,20180110,20180111,20180112,20180113,20180114,20180115,20180116,20180117,20180118,20180119,20180120,20180121,20180122,20180123,20180124,20180125,20180126,20180127,20180128,20180129,20180130,20180131,20180201,20180202,20180203,20180204,20180205,20180206,20180207,20180208'.

Using .replace('2018','2018-'), or re.sub('2018', '2018-', foo_string) from the module re, a date changes to 2018-0101. However, I'd like to insert a hyphen for a range of dates. I have tried the following re.sub('{}'.format(range(2018,2019)), '2018-', foo_string) to no avail. The string is returned without any changes. For reference the years I'd like to range over for the hyphen are 1981 to 2018.

Additionally, I'm unsure of a way to insert a hyphen between the month and dates in a clean way. Help along these directions would be greatly appreciated.

2
  • So you want 20180101 to be 2018-01-01? Commented Feb 18, 2018 at 11:57
  • @heemayl Yes, all dates are in this format. Commented Feb 18, 2018 at 11:59

5 Answers 5

4

I think that using datetime will be more pythonic,

from datetime import datetime

date_list = '20180101,20180102,20180103,20180104,20180105,20180106,20180107,20180108'

def new_date_format(old_format_str):
    date_format = datetime.strptime(old_format_str, '%Y%m%d')
    new_format = date_format.strftime("%Y-%m-%d")
    return new_format

for date_str in date_list.split(","):
    print(new_date_format(date_str))

This will produce the following output:

2018-01-01
2018-01-02
2018-01-03
2018-01-04
2018-01-05
2018-01-06
2018-01-07
2018-01-08
Sign up to request clarification or add additional context in comments.

Comments

1

I would do

def insert_hyphen(stri):
    return ",".join([date[:4]+"-"+date[4:] for date in stri.split(',')])

if you want double hyphens then do

def insert_hyphen(stri):
    return ",".join([date[:4]+"-"+date[4:6]+"-"+date[6:] for date in stri.split(',')])

1 Comment

Works better if you combine KGSH's answer with yours, i.e., def insert_hyphen(stri): return ",".join([date[:4:] + '-' + date[4:6:] + '-' + date[6::] for date in stri.split(',')])
1

If your list consists of dates, why not store your list as dates?

Python has a datetime module which facilitates just this:

from datetime import datetime

lst = '20180101,20180102'

# first convert to datetime
lst = [datetime.strptime(i, '%Y%M%d') for i in lst.split(',')]

# define year combiner
def year_range(lst, year_start, year_end):
    return [i.replace(year=j) for j in range(year_start, year_end+1) for i in lst]

# make list for year range from lst
lst = year_range(lst, 2015, 2018)

# [datetime.datetime(2015, 1, 1, 0, 1),
#  datetime.datetime(2016, 1, 1, 0, 1),
#  datetime.datetime(2017, 1, 1, 0, 1),
#  datetime.datetime(2018, 1, 1, 0, 1),
#  datetime.datetime(2015, 1, 2, 0, 1),
#  datetime.datetime(2016, 1, 2, 0, 1),
#  datetime.datetime(2017, 1, 2, 0, 1),
#  datetime.datetime(2018, 1, 2, 0, 1)]

Comments

1

if you DO need to use the re module, you just match the sequence of numbers and replace them with what you find intermixed with the hyphens:

import re

string = '20180101,20180102,20180103,20180104,20180105,20180106,20180107,20180108,20180109,20180110,20180111,20180112,20180113,20180114,20180115,20180116,20180117,20180118,20180119,20180120,20180121,20180122,20180123,20180124,20180125,20180126,20180127,20180128,20180129,20180130,20180131,20180201,20180202,20180203,20180204,20180205,20180206,20180207,20180208'

result = re.sub('([0-9]{4})([0-9]{2})([0-9]{2})','\\1-\\2-\\3',string,0)

print(result)

Comments

0

Use this:

string = '20180203'
new_string = string[:4:] + '-' + string[4:6:] + '-' + string[6::]
print (new_string)

Result:

2018-02-03

Just make sure that the date is in the format YYYYMMDD.

Comments

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.