Skip to content

Instantly share code, notes, and snippets.

@eclecticmiraclecat
Created July 6, 2020 03:23
Show Gist options
  • Save eclecticmiraclecat/71b738b13a4d89b848b402e41444e656 to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/71b738b13a4d89b848b402e41444e656 to your computer and use it in GitHub Desktop.

get the last word

>>> log = '81.107.39.38 -  ... "GET /ply/ HTTP/1.1" 200 7587'
>>> 
>>> log.rsplit()
['81.107.39.38', '-', '...', '"GET', '/ply/', 'HTTP/1.1"', '200', '7587']
>>> 
>>> log.rsplit(None)
['81.107.39.38', '-', '...', '"GET', '/ply/', 'HTTP/1.1"', '200', '7587']
>>> 
>>> log.rsplit(None, 1)
['81.107.39.38 -  ... "GET /ply/ HTTP/1.1" 200', '7587']
>>> 
>>> log.rsplit(None, 1)[1]
'7587'

string splitting

>>> addr = '350 N STATE ST'
>>> parts = addr.split()
>>> parts
['350', 'N', 'STATE', 'ST']
>>> num = parts[0]
>>> parts[0] = num[:-2] + 'XX'
>>> parts
['3XX', 'N', 'STATE', 'ST']
>>> ' '.join(parts)
'3XX N STATE ST'

strip out the comments in violations

>>> v = 'WANTED WORDS - Comments: boo hoo'
>>> v[:v.find('- Comments:')]
'WANTED WORDS '
>>> v[:v.find('- Comments:')].strip()
'WANTED WORDS'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment