Skip to main content
 首页 » 编程设计

python之从大字符串中拆分时间的出现

2025年05月04日14jpfss

在我的任务中,我只想获取时间并存储在变量中,在我的字符串中,时间可能出现超过 1 次,它可能是“AM”或“PM”

我只想从我的字符串中存储这个值。 “4:19:27”和“7:00:05”出现的时间可能不止两次。

str = """ 16908310=android.widget.TextView@405ed820=Troubles | 2131034163=android.widget.TextView@405eec00=Some situations can be acknowledged using the 'OK' button, if present. A green check-mark after the description indicates that the situation has been acknowledged.  Some situations have further detail available by pressing on the text or icon of the trouble message. | 2131034160=android.widget.TextView@407e5380=Zone Perl Thermostatyfu Communication Failure | 2131034161=android.widget.RadioButton@4081b4f8=OK | 2131034162=android.widget.TextView@4082ac98=Sep 12, 2017 4:19:27 AM | 2131034160=android.widget.TextView@40831690=Zone Door Tampered | 2131034161=android.widget.RadioButton@4085bb78=OK | 2131034162=android.widget.TextView@407520c8=Sep 12, 2017 7:00:05 PM |  VIEW : -1=android.widget.LinearLayout@405ec8c0 | -1=android.widget.FrameLayout@405ed278 | 16908310=android.widget.TextView@405ed820 | 16908290=android.widget.FrameLayout@405ee4d8 | -1=android.widget.LinearLayout@405ee998 | 2131034163=android.widget.TextView@405eec00 | -1=android.widget.ScrollView@405ef4f8 | 2131034164=android.widget.TableLayout@405f0200 | 2131034158=android.widget.TableRow@406616d8 | 2131034159=android.widget.ImageView@4066cec8 | 2131034160=android.widget.TextView@407e5380 | 2131034161=android.widget.RadioButton@4081b4f8 | 2131034162=android.widget.TextView@4082ac98 | 2131034158=android.widget.TableRow@4075e3c8 | 2131034159=android.widget.ImageView@4079bc80 | 2131034160=android.widget.TextView@40831690 | 2131034161=android.widget.RadioButton@4085bb78 | 2131034162=android.widget.TextView@407520c8 | -1=com.android.internal.policy.impl.PhoneWindow$DecorView@405ec0c8 |  BUTTONS : 2131034161=android.widget.RadioButton@4081b4f8 | 2131034161=android.widget.RadioButton@4085bb78 | """ 

我的代码是

str = '''TEXT VIEW : 16908310=android.widget.TextView@405ee2f0=Troubles | 2131034163=android.widget.TextView@405ef6d0=Some situations can be acknowledged using the 'OK' button, if present. A green check-mark after the description indicates that the situation has been acknowledged.  Some situations have further detail available by pressing on the text or icon of the trouble message. | 2131034160=android.widget.TextView@40630608=Zone Perl Thermostatyfu Communication Failure | 2131034161=android.widget.RadioButton@40631068=OK | 2131034162=android.widget.TextView@40632078=Sep 12, 2017 4:19:27 AM |  VIEW : -1=android.widget.LinearLayout@405ed390 | -1=android.widget.FrameLayout@405edd48 | 16908310=android.widget.TextView@405ee2f0 | 16908290=android.widget.FrameLayout@405eefa8 | -1=android.widget.LinearLayout@405ef468 | 2131034163=android.widget.TextView@405ef6d0 | -1=android.widget.ScrollView@405effc8 | 2131034164=android.widget.TableLayout@405f0cd0 | 2131034158=android.widget.TableRow@4062f7a8 | 2131034159=android.widget.ImageView@4062fcd0 | 2131034160=android.widget.TextView@40630608 | 2131034161=android.widget.RadioButton@40631068 | 2131034162=android.widget.TextView@40632078 | -1=com.android.internal.policy.impl.PhoneWindow$DecorView@405ecb98 |  BUTTONS : 2131034161=android.widget.RadioButton@40631068 |''' 
 
if " AM "  or " PM " in str:  
    Time = str.split(" AM " or " PM ")[0].rsplit(None, 1)[-1] 
    print Time 

请您参考如下方法:

请注意,您不应该使用诸如 str 之类的特殊词来命名变量。您可以使用正则表达式,如下所示:

import re 
my_string = """ 16908310=android.widget.TextView@405ed820=Troubles | 2131034163=android.widget.TextView@405eec00=Some situations can be acknowledged using the 'OK' button, if present. A green check-mark after the description indicates that the situation has been acknowledged. Some situations have further detail available by pressing on the text or icon of the trouble message. | 2131034160=android.widget.TextView@407e5380=Zone Perl Thermostatyfu Communication Failure | 2131034161=android.widget.RadioButton@4081b4f8=OK | 2131034162=android.widget.TextView@4082ac98=Sep 12, 2017 4:19:27 AM | 2131034160=android.widget.TextView@40831690=Zone Door Tampered | 2131034161=android.widget.RadioButton@4085bb78=OK | 2131034162=android.widget.TextView@407520c8=Sep 12, 2017 7:00:05 PM | VIEW : -1=android.widget.LinearLayout@405ec8c0 | -1=android.widget.FrameLayout@405ed278 | 16908310=android.widget.TextView@405ed820 | 16908290=android.widget.FrameLayout@405ee4d8 | -1=android.widget.LinearLayout@405ee998 | 2131034163=android.widget.TextView@405eec00 | -1=android.widget.ScrollView@405ef4f8 | 2131034164=android.widget.TableLayout@405f0200 | 2131034158=android.widget.TableRow@406616d8 | 2131034159=android.widget.ImageView@4066cec8 | 2131034160=android.widget.TextView@407e5380 | 2131034161=android.widget.RadioButton@4081b4f8 | 2131034162=android.widget.TextView@4082ac98 | 2131034158=android.widget.TableRow@4075e3c8 | 2131034159=android.widget.ImageView@4079bc80 | 2131034160=android.widget.TextView@40831690 | 2131034161=android.widget.RadioButton@4085bb78 | 2131034162=android.widget.TextView@407520c8 | -1=com.android.internal.policy.impl.PhoneWindow$DecorView@405ec0c8 | BUTTONS : 2131034161=android.widget.RadioButton@4081b4f8 | 2131034161=android.widget.RadioButton@4085bb78 | """ 
pattern = '\d{1,2}:\d{2}:\d{2}\s[AP]M' 
date_list = re.findall(pattern, my_string) 
print(date_list) 
# outputs ['4:19:27 AM', '7:00:05 PM'] 

模式解释:

  • \d{1,2} 匹配一个或两个数字
  • :匹配“:”
  • \d{2} 正好匹配两位数字
  • :匹配“:”
  • \d{2} 正好匹配两位数字
  • \s 匹配一个空格
  • [AP] 匹配 A 或 P,只有一个
  • M,最后一个M