从大字符串中分解出现的时间

Sac*_*hya 1 python string split

在我的任务中,我想只获取时间并存储在变量中,在我的字符串中,时间可能超过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 | """
Run Code Online (Sandbox Code Playgroud)

我的代码是

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
Run Code Online (Sandbox Code Playgroud)

fra*_*ima 5

请注意,您不应该使用特殊字来命名变量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']
Run Code Online (Sandbox Code Playgroud)

模式说明:

  • \ d {1,2}匹配一个或两个数字
  • : 火柴 ":"
  • \ d {2}恰好匹配两位数
  • : 火柴 ":"
  • \ d {2}恰好匹配两位数
  • \ s匹配一个空格
  • [AP]匹配A或P,只匹配一个
  • M,最后一个M.