1

I have a file in which each line contains one timestamp as a part of that line. The timestamp format is 1996-07-04 00:00:00.0 . I want to convert this to 1996-07-04 00:00:00 without the millisecond in each line. I tried using re.sub() method in Python but it replaces it with the value given by me and does not retain the original timestamp. I was using

re.sub("(\d\d\d\d-\d\d-\d\d\s+\d\d:\d\d:\d\d.\d)", "replace without millisec", cell)

The 2nd parameter is my problem.

3
  • post your attempts.. Commented May 21, 2015 at 9:01
  • If the timestamps are strings can you not do slicing [:-2]? Commented May 21, 2015 at 9:01
  • 2
    Basically, nobody won't be able to help you if you don't provide the faulting regexp... Commented May 21, 2015 at 9:01

1 Answer 1

4

You can use the following regex that will capture what you need to keep, and then use the backreference to restore it after a sub replacement:

\b(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})\.\d+\b

Replace with \1.

See demo

IDEONE code:

import re
p = re.compile(r'\b(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})\.\d+\b')
test_str = u"1996-07-04 00:00:00.0"
print re.sub(p, r"\1", test_str)

Note that you do not have to repeat the same subpatterns like \d\d\d\d, just use a limiting quantifier {n} where n is the number of times you need the subpattern to repeat. You can even set minimum and maximum boundaries like {1,4}, or just the minimum {2,}.

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

2 Comments

Thanks! This works. Can you tell what does r"\1" does? I am new to python.
Please check the official re.sub documentation: It is a backreference to the text captured by the first capturing group (the subpattern inside the first group of round brackets). You can even use \g<1> instead of just \1.

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.