草庐IT

Strptime

全部标签

python - datetime.strptime() 为 %Z 接受哪些可能的值?

Python的datetime.strptime()被记录为支持%Z字段中的时区。所以,例如:In[1]:datetime.strptime('2009-08-1914:20:36UTC',"%Y-%m-%d%H:%M:%S%Z")Out[1]:datetime.datetime(2009,8,19,14,20,36)但是,“UTC”似乎是我可以让它支持的唯一时区:In[2]:datetime.strptime('2009-08-1914:20:36EDT',"%Y-%m-%d%H:%M:%S%Z")ValueError:timedata'2009-08-1914:20:36EDT'd

python - 为什么 python 的 datetime.datetime.strptime ('201412' , '%Y%m%d' ) 不引发 ValueError?

在我给出的格式中,日期2014-01-02将由“20140102”表示。这是使用标准strptime正确解析的:>>>datetime.datetime.strptime("20140102","%Y%m%d")datetime.datetime(2014,1,2,0,0)在这种格式中,“201412”不是有效日期。docs假设“%m”指令是“以零填充的十进制数表示的月份”。它给出了示例“01、02、...、12”。days指令“%d”也应该用零填充。基于此,我预计“201412”将是这种格式的无效输入,因此会引发ValueError。相反,它被解释为2014-01-02:>>>dat

python - 为什么 `datetime.strptime` 得到的 2015 年第 0 周星期二的日期不正确?

我在pythondatetime.strptime函数中发现了一个错误。我已经根据周数(%W)、年(%Y)和星期几(%w)。2015年第一周星期二日期错误:>>>fromdatetimeimportdatetime>>>datetime.strptime('%s%s%s'%(0,2015,1),'%W%Y%w').date()datetime.date(2014,12,29)#OK>>>datetime.strptime('%s%s%s'%(0,2015,2),'%W%Y%w').date()datetime.date(2015,1,1)#WRONG!!!>>>datetime.str

python - datetime.datetime.strptime 在 Python 2.4.1 中不存在

在某些情况下,我们的团队需要使用Python2.4.1。strptime不存在于Python2.4.1的datetime.datetime模块中:Python2.4.1(#65,Mar302005,09:13:57)[MSCv.131032bit(Intel)]Type"help","copyright","credits"or"license"formoreinformation.>>>importdatetime>>>datetime.datetime.strptimeTraceback(mostrecentcalllast):File"",line1,inAttributeErr

python - 更快的 strptime?

我有代码以“YYYY-MM-DD”格式读取大量日期。解析所有这些日期,以便它可以添加一天、两天或三天,然后以相同的格式写回,这会大大降低速度。321465714.3300.000103.6980.000trade.py:56(effective)321841834.7570.00066.1550.000_strptime.py:295(_strptime)day=datetime.datetime.strptime(endofdaydate,"%Y-%m-%d").date()关于如何加快一点(或很多)速度有什么建议吗? 最佳答案

datetime.strptime() 的 Python 时区 '%z' 指令不可用

使用datetime.strptime()的'%z'模式我有一个代表日期的字符串文本,我完全能够解析它并将其转换为一个干净的日期时间对象:date="[24/Aug/2014:17:57:26"dt=datetime.strptime(date,"[%d/%b/%Y:%H:%M:%S")除了我无法使用指定的%z模式捕获具有时区的整个日期字符串heredate_tz=24/Aug/2014:17:57:26+0200dt=datetime.strptime(date,"[%d/%b/%Y:%H:%M:%S%z]")>>>ValueError:'z'isabaddirectiveinfor

python datetime strptime 通配符

我想将这样的日期解析为日期时间对象:2008年12月12日2009年1月1日以下内容适用于第一次约会:datetime.strptime("December12th,2008","%B%dth,%Y")但由于日期编号('st')的后缀,第二次会失败。那么,strptime中是否存在未记录的通配符?还是更好的方法? 最佳答案 尝试使用dateutil.parser模块。importdateutil.parserdate1=dateutil.parser.parse("December12th,2008")date2=dateutil.

python - 将 unicode 转换为 datetime 正确的 strptime 格式

我正在尝试将unicode对象转换为datetime对象。我通读了文档:http://docs.python.org/2/library/time.html#time.strptime试过了datetime.strptime(date_posted,'%Y-%m-%dT%H:%M:%SZ')但我收到错误消息ValueError:timedata'2014-01-15T01:35:30.314Z'doesnotmatchformat'%Y-%m-%dT%H:%M:%SZ'对什么是正确的格式有任何反馈吗?我很感激时间和专业知识。 最佳答案

python - “模块”对象没有属性 '_strptime',有多个线程 Python

我收到此错误'module'objecthasnoattribute'_strptime'但仅当我使用多个线程时。当我只使用一个时,它工作正常。我使用python2.7x64。这里是我调用的简化函数importdatetimedefget_month(time):returndatetime.datetime.strptime(time,'%Y-%m-%dT%H:%M:%S+0000').strftime("%B").lower()这是完整的回溯:AttributeError:'module'objecthasnoattribute'_strptime'Exceptioninthrea

python - 线程安全等效于 python 的 time.strptime()?

我写的东西在线程中使用time.strptime()时会抛出很多AttributeError异常。这似乎只发生在Windows(而不是Linux)上,但无论如何——在谷歌上搜索时,似乎time.strptime()不被认为是线程安全的。有没有更好的方法从字符串创建日期时间对象?当前代码如下:val=DateFromTicks(mktime(strptime(val,'%B%d,%Y')))但是,当它在线程内运行时会产生异常。谢谢! 最佳答案 根据bugreport,如果您在创建线程之前调用strptime一次,则不会发生这种情况。我