This question already has answers here:
Convert an RFC 3339 time to a standard Python timestamp
(15 answers)
Closed 7 years ago.
i have a timestamp that looks like this 2015-11-06T14:20:14.011+01:00. I would like to parse it to datetime.
I have the idea that i can use %Y-%m-%dT%H:%M:%S.%f%z as representation of this.
But the problem is the colon in the timezone. How can i remove the colon in the Timezone or is there a better way as the %z?
You have an ISO 8601 datetime string. Don't bother parsing it or fiddling with it by hand (see: XY Problem). Use the iso8601 library for Python.
import iso8601
parsed = iso8601.parse_date("2015-11-06T14:20:14.011+01:00")
If you want to remove the timezone information from it, use the replace method.
tz_stripped = parsed.replace(tzinfo=None)
import re
original = '2015-11-06T14:20:14.011+01:00'
replaced = re.sub(r'([+-]\d+):(\d+)$', r'\1\2', original)
# replaced == '2015-11-06T14:20:14.011+0100'
This will replace the colon only when it is preceded by a plus or minus, and surrounded by digits until the end of the string.
I think the best way to do this is with dateutils
https://labix.org/python-dateutil
from dateutil.parser import parse
original = '2015-11-06T14:20:14.011+01:00'
print "Original Date {}".format(original)
new_date = parse(original)
print new_date
print type(new_date)
# print new_date.hour
# print new_date.minute
# print new_date.second
print "New Date 1 {}".format(new_date.strftime('%Y-%m-%d %H:%M:%S'))
print "New Date 2 {}".format(new_date.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
Output:
Original Date 2015-11-06T14:20:14.011+01:00
2015-11-06 14:20:14.011000+01:00
<type 'datetime.datetime'>
New Date 1 2015-11-06 14:20:14
New Date 2 2015-11-06T14:20:14.011000+0100
Regards
Related
Silly Question, but couldn't find the proper answer.
I converted my datetime object from UTC to ist using dateutils.
utc = datetime.utcnow()
UTC = tz.gettz('UTC')
indian = tz.gettz('Asia/Kolkata')
ind = utc.replace(tzinfo=UTC)
ind.astimezone(indian).replace(microsecond=0).__str__()
Output
'2019-07-30 16:32:04+05:30'
I would like to remove the +5:30 part, how do I go about doing that, except splitting the string on '+' symbol, or how do I avoid it being added in the first place.
You can explicitly state your format via strftime
>>> new = ind.astimezone(indian).replace(microsecond=0)
>>> new.strftime('%Y %m %d %H:%M:%S')
'2019 07 30 16:59:56'
You can simply strip out the timezone from the datetime object by using tzinfo=None. The string representation will still be ISO8601, but with no timezone offset part.
str(
datetime.now(tz=tz.gettz('Asia/Kolkata')))
.replace(microseconds=0, tzinfo=None)
)
# '2019-07-30 16:32:04'
string1 = "2018-Feb-23-05-18-11"
I would like to replace a particular pattern in a string.
Output should be 2018-Feb-23-5-18-11.
How can i do that by using re.sub ?
Example:
import re
output = re.sub(r'10', r'20', "hello number 10, Agosto 19")
#hello number 20, Agosto 19
Fetching the current_datetime from datetime module. i'm formatting the obtained datetime in a desired format.
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime("%Y-%b-%d-%I-%M-%S")
I thought, re.sub is the best way to do that.
ex1 :
string1 = "2018-Feb-23-05-18-11"
output : 2018-Feb-23-5-18-11
ex2 :
string1 = "2018-Feb-23-05-8-11"
output : 2018-Feb-23-5-08-11
When working with dates and times, it is almost always best to convert the date first into a Python datetime object rather than trying to attempt to alter it using a regular expression. This can then be converted back into the required date format more easily.
With regards to leading zeros though, the formatting options only give leading zero options, so to get more flexibility it is sometimes necessary to mix the formatting with standard Python formatting:
from datetime import datetime
for test in ['2018-Feb-23-05-18-11', '2018-Feb-23-05-8-11', '2018-Feb-1-0-0-0']:
dt = datetime.strptime(test, '%Y-%b-%d-%H-%M-%S')
print '{dt.year}-{}-{dt.day}-{dt.hour}-{dt.minute:02}-{dt.second}'.format(dt.strftime('%b'), dt=dt)
Giving you:
2018-Feb-23-5-18-11
2018-Feb-23-5-08-11
2018-Feb-1-0-00-0
This uses a .format() function to combine the parts. It allows objects to be passed and the formatting is then able to access the object's attributes directly. The only part that needs to be formatted using strftime() is the month.
This would give the same results:
import re
for test in ['2018-Feb-23-05-18-11', '2018-Feb-23-05-8-11', '2018-Feb-1-0-0-0']:
print re.sub(r'(\d+-\w+)-(\d+)-(\d+)-(\d+)-(\d+)', lambda x: '{}-{}-{}-{:02}-{}'.format(x.group(1), int(x.group(2)), int(x.group(3)), int(x.group(4)), int(x.group(5))), test)
Use the datetime module.
Ex:
import datetime
string1 = "2018-Feb-23-05-18-11"
d = datetime.datetime.strptime(string1, "%Y-%b-%d-%H-%M-%S")
print("{0}-{1}-{2}-{3}-{4}-{5}".format(d.year, d.strftime("%b"), d.day, d.hour, d.minute, d.second))
Output:
2018-Feb-23-5-18-11
I'm familiar with dateutil.parser which allows one to parse a string representing a time into a datetime object. What I would like to do, however, is to 'search' for such a 'time string' within a larger string representing an interval of time. For example:
from datetime import timedelta
import dateutil.parser
import parse
start = dateutil.parser.parse("5 Nov 2016 15:00")
end = start + timedelta(hours=1)
string = "from {start} till {end}".format(start=start, end=end)
start_pattern = "from {:tg}"
result = parse.search(start_pattern, string)
I'd like to recover the start and end as datetime objects based on the fact that they follow the words "from" and "till", respectively.
Here I have tried to use the parse module, but the format specifier :tg (for global time syntax) doesn't seem to work on datetime's default string representation, nor do the other available ones look similar to the one in string.
What would be a simple and elegant way to parse back the start and end in this example?
The re package could help you in this case; just make regular expressions for the strings you want to match, and use them to extract the date part.
I found a way to do it using a regular expression:
from datetime import timedelta
import dateutil.parser
import re
start = dateutil.parser.parse("5 Nov 2016 15:00")
end = start + timedelta(hours=1)
string = "from {start} till {end}".format(start=start, end=end)
pattern = '(?:\s*from\s*)' + '(?P<start>.+)' + '(?:\s*till\s*)' + '(?P<end>.+)' + '(?:\s*)'
groups = re.match(pattern, string).groupdict()
parsed_start = dateutil.parser.parse(groups['start'])
parsed_end = dateutil.parser.parse(groups['end'])
assert parsed_start == start
assert parsed_end == end
i am trying to do string manipulation based on format. str.replace(old,new) alllows changing by specific string pattern. is it possible to find and replace by format? for example,
i want to find all datetime like value in a long string and replace it with another format
assuming % is wildcard for number and datetime is %%/%%/%%T%%:%%
str.replace(%%/%%/%%T%%:%%, 'dummy value')
EDIT:
sorry i should have been more clearer. re.sub seems like I can use that, but how do it substitute it with a date converted value. in this case, e.g.
YY/MM/DDTHH:MM to (YY/MM/DD HH:MM)+8 hours
The easiest way to do this is probably using a combination of regular expression syntax, applying re.sub and using the fact that the repl parameter can be a function that takes a match and returns a string to replace it, and datetime's syntax for strptime and strftime:
>>> from datetime import datetime
>>> import re
>>> def replacer(match):
return datetime.strptime(
match.group(), # matched text
'%y/%m/%dT%H:%M', # source format in datetime syntax
).strftime('%d %B %Y at %H.%M') # destination format in datetime syntax
>>> re.sub(
r'\d{2}/\d{2}/\d{2}T\d{2}:\d{2}', # source format in regex syntax
replacer, # function to process match
'The date and time was 12/12/12T12:12 exactly.', # string to process
)
'The date and time was 12 December 2012 at 12.12 exactly.'
The only downside of this is that you need to define the source format in both datetime and re syntax, which isn't very DRY; if they don't match, you'll get nowhere.
I'm trying to filter a date retrieved from a .csv file, but no combination I try seems to work. The date comes in as "2011-10-01 19:25:01" or "year-month-date hour:min:sec".
I want just the year, month and date but I get can't seem to get ride of the time from the string:
date = bug[2] # Column in which the date is located
date = date.replace('\"','') #getting rid of the quotations
mdate = date.replace(':','')
re.split('$[\d]+',mdate) # trying to get rid of the trailing set of number (from the time)
Thanks in advance for the advice.
If your source is a string, you'd probably better use strptime
import datetime
string = "2011-10-01 19:25:01"
dt = datetime.datetime.strptime(string, "%Y-%m-%d %H:%M:%S")
After that, use
dt.year
dt.month
dt.day
to access the data you want.
Use datetime to parse your input as a datetime object, then output it in whatever format you like: http://docs.python.org/library/datetime.html
I think you're confusing the circumflex for start of line and dollar for end of line. Try ^[\d-]+.
If the format is always "YYYY-MM-DD HH:mm:ss", then try this:
date = date[1:11]
In a prompt:
>>> date = '"2012-01-12 15:13:20"'
>>> date[1:11]
'2012-01-12'
>>>
No need for regex
>>> date = '"2011-10-01 19:25:01"'
>>> date.strip('"').split()[0]
'2011-10-01'
One problem with your code is that in your last regular expression, $ matches the end of the string, so that regular expression will never match anything. You could do this much more simply by splitting by spaces and only taking the first result. After removing the quotation marks, the line
date.split()
will return ["2011-10-01", "19:25:01"], so the first element of that list is what you need.