English 中文(简体)
用于确定时间投入(只有Hr、Min、Sec)的散射方式
原标题:Pythonic way to Validate time input (only for Hr, Min, Sec)

在我的申请中,我正在获得时间投入,其形式是:Values = 12,12,12

Now i need to validate it h<24 M<60 S<60 etc. and i want final output in %H:%M:%S format.
To get this i tried datetime.time().

我曾用<代码>价值 = 12 然后用<代码>12,12,尝试过。

In [1]: import datetime

In [2]: values =  12 

In [3]: d = datetime.time(values)

TypeError  Traceback (most recent call last)
/mypc/test/<ipython console> in <module>()
TypeError: an integer is required

In [4]: d = datetime.time(int(values))

In [5]: d
Out[5]: datetime.time(12, 0)

In [6]: d.strftime( %H:%M:%S )
Out[6]:  12:00:00 

In [7]: s = d.strftime( %H:%M:%S )

In [8]: s
Out[8]:  12:00:00 

In [9]: values =  12,12,12 

In [10]: d = datetime.time(int(values))

ValueError: invalid literal for int() with base 10:  12,12,12 

但它的工作如下。

In [24]: datetime.time(12,12,12).strftime( %H:%M:%S )
Out[24]:  12:12:12 

因此,问题是这一时间。 时间()作为星体和<代码>12,12,的输入不能按字母换算。

Is there any other way(otherthan regexp) to do the validation for only Hr:M:S.

最佳回答

You have to unpack the values:

>>> values =  12,12,12 
>>> values = ast.literal_eval(values)
>>> datetime.time(*values)
datetime.time(12, 12, 12)

如果给付的时间无效,最后一份声明将产生错误。

为了避免出现零增量的问题,正如“wim”所指出的,有可能将第二行改为:

values = (int(i) f或i in values.split( , ))

values = map(int, values.split( , ))
问题回答

请注意,<代码>strftime的对口单位为strptime,即,如果你需要一段时间才能使用strptime。 采用与你使用<代码>strftime相同的格式,用该格式印刷一段时间:

>>> import time
>>> t = time.strptime( 12,12,12 ,  %H,%M,%S )
>>> time.strftime( %H:%M:%S , t)
 12:12:12 

这样,strptime (H<24, M<60, S<=61):

>>> time.strptime( 24,0,0 ,  %H,%M,%S )
...
ValueError: time data  24,0,0  does not match format  %H,%M,%S 
>>> time.strptime( 0,60,0 ,  %H,%M,%S )
...
ValueError: time data  0,60,0  does not match format  %H,%M,%S 
>>> time.strptime( 0,0,62 ,  %H,%M,%S )
...
ValueError: unconverted data remains: 2

Note that strptime allows S<=61 as explained in the documentation:

The range really is 0 to 61; this accounts for leap seconds and the (very rare) double leap seconds.

如果这给你带来问题,那么你很可能需要将这一价值纳入你的法典。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签