Let’s say a fella wanted to parse out a (US) city, state, and 9 digit zipcode from a string that had them run together, Something like: “DOVER, FL 33527-4102”.
I’m currently doing this:
csz = "DOVER, FL 33527-4102" m1 = re.search(r'.+ ([A-Z][A-Z]) ([0-9]{5}-[0-9]{4})',csz) if (m1): (city,state,zipcode) = m1.groups() print("City:",city) print("State:",state) print("Zipcode:",zipcode) else: print("Failed") #Is there a better way to do this. Can it be done in fewer lines?
submitted by /u/Benign_Variant
[link] [comments]
r/learnpython Let’s say a fella wanted to parse out a (US) city, state, and 9 digit zipcode from a string that had them run together, Something like: “DOVER, FL 33527-4102”. I’m currently doing this: csz = “DOVER, FL 33527-4102” m1 = re.search(r’.+ ([A-Z][A-Z]) ([0-9]{5}-[0-9]{4})’,csz) if (m1): (city,state,zipcode) = m1.groups() print(“City:”,city) print(“State:”,state) print(“Zipcode:”,zipcode) else: print(“Failed”) #Is there a better way to do this. Can it be done in fewer lines? submitted by /u/Benign_Variant [link] [comments]
Let’s say a fella wanted to parse out a (US) city, state, and 9 digit zipcode from a string that had them run together, Something like: “DOVER, FL 33527-4102”.
I’m currently doing this:
csz = "DOVER, FL 33527-4102" m1 = re.search(r'.+ ([A-Z][A-Z]) ([0-9]{5}-[0-9]{4})',csz) if (m1): (city,state,zipcode) = m1.groups() print("City:",city) print("State:",state) print("Zipcode:",zipcode) else: print("Failed") #Is there a better way to do this. Can it be done in fewer lines?
submitted by /u/Benign_Variant
[link] [comments]