Skip to main content
 首页 » 编程设计

Python:测试 unicode,并转换为 time()

2025年05月04日19powertoolsteam

有时 self.start 是 unicode:

例如。

>>>self.start 
u'07:30:00' 

这让 datetime.combine 提示

start = datetime.combine(self.job_record.date, self.start) 

如何做:

  1. 测试 unicode?
  2. 将 u'07:30:00' 转换为 datetime.time?

请您参考如下方法:

检查 unicode:

>>> import types 
>>> type(u'07:30:00') is types.UnicodeType 
True 
>>> type('regular string') is types.UnicodeType 
False 

将字符串转换为时间:

>>> import time 
>>> time.strptime(u'07:30:00', '%H:%M:%S') 
(1900, 1, 1, 7, 30, 0, 0, 1, -1)