2

I have a pipe delimited text file with records like:

ABC|1234|10/26/2016|PQRS|02/27/2016|

GHI|4321|02/27/2016|UOIP|10/26/2016|

Looking for a way to change mm/dd/yyyy format to be changed to yyyy-mm-dd

2
  • are there linebreaks(\n) between the lines? Commented Oct 26, 2016 at 12:13
  • Yes, there is a linebreak at the end of every row Commented Oct 26, 2016 at 12:32

4 Answers 4

1

Use the following approach with strptime and strftime functions from datetime module:

import datetime

# while iterating through the lines with a given format
# ...
line = 'ABC|1234|10/26/2016|PQRS|02/27/2016|'

line = '|'.join([item if k not in [2,4] else datetime.datetime.strptime(item, '%m/%d/%Y').strftime("%Y-%m-%d")
        for k, item in enumerate(line.split('|'))])

print(line)

The output(for exemplary line):

ABC|1234|2016-10-26|PQRS|2016-02-27|

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Sign up to request clarification or add additional context in comments.

1 Comment

@Kshitij, as for your claim : at first, you have changed the initial format, secondly - it works fine and gives the output 1111222|2016-10-26|8888-12-31|ABCD0007|ABCDEFGH|. Check your code for errors
1

Probably not the cleanest way to do it, but you can try the following:

my_string = "ABC|1234|10/26/2016|PQRS|02/27/2016|"

#Split the string with the '|' character and return a list.
string_elements=my_string.split('|')

#The item 2 of the list (which is the first date) is split according to the '/' character
string_elements[2]=string_elements[2].split('/')
#The item 2 is transformed by making a rotation of the element to have the format yyyy-mm-dd and is joined on the character '-'    
string_elements[2]='-'.join(string_elements[2][-1:] + string_elements[2][:-1])

#Same as above for teh item 4 which is the second date
string_elements[4]=string_elements[4].split('/')
string_elements[4]='-'.join(string_elements[4][-1:] + string_elements[4][:-1])

#The list of item is joined with the '|' character to reform a string
my_transformed_string='|'.join(string_elements)
print my_transformed_string

The result is:

ABC|1234|2016-10-26|PQRS|2016-02-27|

Comments

0

Without using any fancy trick, you can achieve it just by using str.split() as:

>>> my_string = "ABC|1234|10/26/2016|PQRS|02/27/2016|"
>>> mm, dd, yy = my_string.split("|")[2].split("/")
>>> print "{}-{}-{}".format(yy, mm, dd)
2016-10-26

1 Comment

How to change the older date format with the new format for all the occurrences of date values in a row?
0

You could also do a regex replacement:

import re

string = """ABC|1234|10/26/2016|PQRS|02/27/2016|

GHI|4321|02/27/2016|UOIP|10/26/2016|"""

dates = re.sub('(\d{1,2})/(\d{1,2})/(\d{4})', '\g<3>/\g<1>/\g<2>', string)

print dates

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.