I have a bunch of strings but I only want to keep the ones with this format:
x/x/xxxx xx:xx
What is the easiest way to check if a string meets this format? (Assuming I want to check by if it has 2 /'s and a ':' )
A format of . 2f (note the f ) means to display the number with two digits after the decimal point. So the number 1 would display as 1.00 and the number 1.5555 would display as 1.56 .
Python's str. format() technique of the string category permits you to try and do variable substitutions and data formatting. This enables you to concatenate parts of a string at desired intervals through point data format.
try with regular expresion:
import re
r = re.compile('.*/.*/.*:.*')
if r.match('x/x/xxxx xx:xx') is not None:
   print 'matches'
you can tweak the expression to match your needs
Use time.strptime to parse from string to time struct. If the string doesn't match the format it raises ValueError.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With