python - 为什么有“空格”隔断的字符串,split切分后取不到值?
PHP中文网
PHP中文网 2017-04-17 16:48:16
0
5
627

根据用户的访问ip回显用户的城市名称:

ip = request.remote_addr
city_name = str(find(ip))

这样ip地址库返回的city_name名称是“中国 安徽 合肥”,“中国 北京 北京”这样的结构;如果ip库找不到对应的城市名称就会显示“中国 福建”。
我不要显示这么长的名称,只要显示“合肥”、“北京”、“福建”这样最后一个的位置即可,修改如下(python):

city_name = str(find(ip)).split(' ').pop(-1)

但是取不到“合肥”、“北京”、“福建”这样的值,是怎么回事呢?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
黄舟

There may be extra spaces at the end
city_name = str(find(ip)).rstrip().split(' ').pop(-1)

洪涛

Is it because of the full-width spaces?

阿神

Split don’t add parameters

小葫芦

Is it an encoding problem? Paste the source code.

阿神

Violent debugging--print

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!