Question: Question: You're working with a dataset on capital cities around the world. This dataset includes a field that containsinformation on both city and country. You'd

Question: You're working with a dataset on capital cities around the world. This dataset includes a field that containsinformation on both city and country. You'd like to separate this field into two fields, a city field and a countryfield. In the current field, city and country are separated by either a comma or a period. Complete the functionparse_city_country to
You're working with a dataset on capital cities around the world. This dataset includes a field that contains
information on both city and country. You'd like to separate this field into two fields, a city field and a country
field. In the current field, city and country are separated by either a comma or a period. Complete the function
parse
_
city
_
country to split city and country into two strings and return only the city.
def parse
_
city
_
country
(
text
)
:
pattern
=
#enter the regex pattern here
result
=
re
.
_
_
(
pattern
,
text
)
#enter the re method here
if len
(
result
)
!
=
2
:
return
"
'
"
return #return the correct capturing group
print
(
parse
_
city
_
country
(
"
Paris
,
France"
)
)
# should return Paris
print
(
parse
_
city
_
country
(
"
Mumbai
,
India"
)
)
# should return Mumbai
print
(
parse
_
city
_
country
(
"
Rio de Janeiro. Brazil"
)
)
# should return Rio de Janeiro
print
(
parse
_
city
_
country
(
"
Tokyo
!
Japan"
)
)
# result should be blank
Run

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!