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

时间:2017-09-12 12:36:57

标签: 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 | """

我的代码是

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

5 个答案:

答案 0 :(得分: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']

模式说明:

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

答案 1 :(得分:1)

regex与此表达式一起使用:([0-9]{1,2}:[0-9]{2}:[0-9]{2}) (AM|PM)。此模式将为您提供两个组:一个用于时间编号,另一个用于AMPM信息。这比手动拆分字符串要好得多。您可以对其进行测试here,并习惯使用regex

总而言之,你可以在python

中使用它
import re

p = re.compile('([0-9]{1,2}:[0-9]{2}:[0-9]{2}) (AM|PM)')
for (numbers, status) in p.match(theString):
    #prints the numbers like 04:02:55
    print(numbers)
    #prints the AM or PM
    print(status)

答案 2 :(得分:1)

使用str作为变量名称不是一个好主意,因为它是内置的 所以假设你的字符串在s,这里是一个交互式演示 我认为你想要什么。

>>> import re
>>> re.findall('[=][^|=]+[AP]M [|]', s)
['=Sep 12, 2017 4:19:27 AM |', '=Sep 12, 2017 7:00:05 PM |']

>>> [r.split() for r in re.findall('[=][^|=]+[AP]M [|]', s)]
[['=Sep', '12,', '2017', '4:19:27', 'AM', '|'], ['=Sep', '12,', '2017', '7:00:05', 'PM', '|']]

>>> [r.split()[3] for r in re.findall('[=][^|=]+[AP]M [|]', s)]
['4:19:27', '7:00:05']

>>> 

答案 3 :(得分:0)

使用regex

会很容易

<script src="//repl.it/embed/Kyqe/0.js"></script>

您可以使用此正则表达式

\d+:\d+:\d+

r'\d{1,2}:\d{1,2}:\d{1,2}'

代码:https://repl.it/Kyqe/0

答案 4 :(得分:0)

正则表达式是你的朋友。例如:

CDataMapping

有问题的正则表达式匹配XX:XX:XX AM和XX:XX:XX PM的任何出现,并考虑到时间为4:00:00 AM和04:00:00 AM。 / p>