Converting string to datetime in Python using strptime

2024/7/8 6:42:55

I'm trying to convert the following String to datetime object in Python.

datetime_object = datetime.strptime('Sat, 26 Nov 2016 15:17:00 +0000', '%a, %b %d %Y %H:%c %z')

I get the following error,

File "<stdin>", line 1, in <module>File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 577, in _strptime_datetimett, fraction, gmtoff_fraction = _strptime(data_string, format)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 342, in _strptimeformat_regex = _TimeRE_cache.compile(format)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 272, in compilereturn re_compile(self.pattern(format), IGNORECASE)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 234, in compilereturn _compile(pattern, flags)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 286, in _compilep = sre_compile.compile(pattern, flags)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_compile.py", line 764, in compilep = sre_parse.parse(p, flags)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 930, in parsep = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 426, in _parse_subnot nested and not items))File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 813, in _parseraise source.error(err.msg, len(name) + 1) from None
re.error: redefinition of group name 'a' as group 6; was group 1 at position 185

What am I doing wrong here?

Answer

You're almost there, but there were a few errors in the directives you were using. Try:

datetime_object = datetime.strptime('Sat, 26 Nov 2016 15:17:00 +0000', '%a, %d %b %Y %H:%M:%S %z')>>> datetime_object
datetime.datetime(2016, 11, 26, 15, 17, tzinfo=datetime.timezone.utc)
https://en.xdnf.cn/q/119527.html

Related Q&A

Is there any differences between python2 and python3 about adding menu bar to Frame in tkinter?

Im trying to porting a project 2to3 on python, and stuck in tkinter.In python2, there is no problem with adding menu bar to Frame in tkinter,but python3 occured attribute error. (Frame object has no at…

Standard Input having weird characters with them in different programming lanuage

I am getting confused with the standard input of these programming languages : [Note:] I added details about so many programming languages as the problem with all of them is same in this matter and my …

How to turn a numpy array to a numpy object?

I have a NumPy array as follows: [[[ 0 0]][[ 0 479]][[639 479]][[639 0]]]and I would like to convert it into something like so: [( 0 0)( 0 479)(639 479)(639 0), dtype=dtype([(x, <i2), (y…

recover all line from an attribute in a database in json

To simplify my problem, I have a base in json, and I recover all of my lines of json to put informations in a base. It seems easy for moments, but problem is that my json is not correctly writtenSo i…

Calculate eigen value in python as same way(order) in Matlab

This is the Matlab code which is returning eigenvector in V and eigenvalue in D. Consider C is 9*9 matrix then V is 9*9 matrix and D is 9*9 diagonal. matrix.[V,D] = eig(C);I want the same thing in Pyth…

Python: ctypes and Pointer to Structure

I am trying to make a pointer of a struct and then de-reference it. But its crashing. I have mimiced the behvior here with this simple code. from ctypes import * import ctypesclass File(Structure):_fie…

Python:Why readline() function doesnt work for file looping

I have the following code:#!/usr/bin/pythonf = open(file,r)for line in f:print line print Next line , f.readline() f.close()This gives the following output:This is the first lineNext line That was the …

How to replace cropped rectangle in opencv?

I have managed to cropped a bounding box with text, e.g. given this image:Im able to exact the following box:with this code: import re import shutilfrom IPython.display import Imageimport requests impo…

How can I read hexadecimal data with python?

I have this c# app that Im trying to cooperate with a app written in python. The c# app send simple commands to the python app, for instance my c# app is sending the following:[Flags]public enum GameRo…

Want to scrape all the specific href from the a tag

I have search the specific brand Samsung , for this number of products are search ,I just wanted to scrape all the href from the of the search products with the product name . enter code here import u…