如何在Robot Framework中从日期时间字符串移除时间部分
解决Robot Framework提取日期、移除时间部分的问题
你当前获取到的设备安装日期字符串是09/06/2023 13:14,想要保留日期、移除时间部分。之前用remove string方法出错,是因为这个关键字只会删除指定的单个字符(空格和冒号),而非移除整个时间段,所以才会得到09/06/20231314这种结果。
以下是几种正确的实现方式:
方法1:按空格分割字符串(最简便)
利用日期和时间之间的空格做分隔,拆分后取第一个元素即可:
${date_time_list}= Split String ${get_deviceinstalldate} ${SPACE} ${only_date}= Set Variable ${date_time_list}[0] log to console Device Install Date:${only_date}
方法2:截取子字符串
先找到空格的位置,再截取该位置之前的内容:
${space_index}= Get Index ${get_deviceinstalldate} ${SPACE} ${only_date}= Get Substring ${get_deviceinstalldate} 0 ${space_index} log to console Device Install Date:${only_date}
方法3:正则表达式提取
如果日期格式固定,用正则匹配提取日期部分:
${only_date}= Evaluate re.search(r'^\d{2}/\d{2}/\d{4}', '${get_deviceinstalldate}').group() modules=re log to console Device Install Date:${only_date}
内容的提问来源于stack exchange,提问作者lng60044
相关产品推荐
相关产品推荐

