python - split occurance of time from large string -


in task want fetch time , store in variable, in string may possible time occurs more 1 time , may "am" or "pm"

i want store value string. "4:19:27" , "7:00:05" occurrence of time may more twice.

str = """ 16908310=android.widget.textview@405ed820=troubles | 2131034163=android.widget.textview@405eec00=some situations can acknowledged using 'ok' button, if present. green check-mark after description indicates situation has been acknowledged.  situations have further detail available pressing on text or icon of 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 | 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 | """ 

my code is

str = '''text view : 16908310=android.widget.textview@405ee2f0=troubles | 2131034163=android.widget.textview@405ef6d0=some situations can acknowledged using 'ok' button, if present. green check-mark after description indicates situation has been acknowledged.  situations have further detail available pressing on text or icon of 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 |  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 " "  or " pm " in str:      time = str.split(" " or " pm ")[0].rsplit(none, 1)[-1]     print time 

note shouldn't name variable special word str. use regular expression, this:

import re my_string = """ 16908310=android.widget.textview@405ed820=troubles | 2131034163=android.widget.textview@405eec00=some situations can acknowledged using 'ok' button, if present. green check-mark after description indicates situation has been acknowledged. situations have further detail available pressing on text or icon of 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 | 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'] 

explanation of pattern:

  • \d{1,2} matches 1 or 2 digits
  • : matches ":"
  • \d{2} matches 2 digits
  • : matches ":"
  • \d{2} matches 2 digits
  • \s matches space
  • [ap] matches either or p, one
  • m, last m

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -