[英]How do I split or grab a string in python after a comma in python
How would you split a string for every comma it pass it For example, let say I want to grab only the BigCity etc 您如何为传递它的每个逗号分割一个字符串例如,假设我只想抓取BigCity等
s="1234 Somestreet Road, BigCity, SomeProvince, A1B 1C1" s =“ 1234 Somestreet Road,BigCity,SomeProvince,A1B 1C1”
so, city = s.split? 因此,city = s.split? How would i do it?
我该怎么办?
I would do something like this: 我会做这样的事情:
x=s.split(',')
if len(x)>1:
x=x[1:] # since you are only concerned with what comes after a ','
print x[0].strip() # remove the strip(), depends on the usage
else:
print ''
s="1234 Somestreet Road" => empty string s =“ 1234 Somestreet Road” =>空字符串
s="1234 Somestreet Road," => empty string s =“ 1234 Somestreet Road,” =>空字符串
s="1234 Somestreet Road, BigCity, SomeProvince, A1B 1C1" => BigCity s =“ 1234 Somestreet Road,BigCity,SomeProvince,A1B 1C1” => BigCity
In [247]: s="1234 Somestreet Road, BigCity, SomeProvince, A1B 1C1"
In [248]: s.split(',')
Out[248]: ['1234 Somestreet Road', ' BigCity', ' SomeProvince', ' A1B 1C1']
In [249]: s.split(',')[1]
Out[249]: ' BigCity'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.