Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从区域中提取区域名称的Python方法(AWS/GCloud)_Python_List_Extract - Fatal编程技术网

从区域中提取区域名称的Python方法(AWS/GCloud)

从区域中提取区域名称的Python方法(AWS/GCloud),python,list,extract,Python,List,Extract,我是Python新手,我想知道,从区域名称中提取区域名称的最佳方法是什么 例如: GCP: - us-east1-c - europe-west2-b AWS: - eu-west-2 - ap-northeast-1 在bash中,我将使用: echo "zone"|rev|cut -f2- -d-|rev" 在Python中,我使用了: '-'.join(zone.split('-')[:-1]), 我知道这并不重要,但我想用蟒蛇的方式来做 提前谢谢 哦,如果区域为us-

我是Python新手,我想知道,从区域名称中提取区域名称的最佳方法是什么

例如:

GCP:

 - us-east1-c
 - europe-west2-b

AWS:

 - eu-west-2
 - ap-northeast-1
在bash中,我将使用:

echo "zone"|rev|cut -f2- -d-|rev"
在Python中,我使用了:

'-'.join(zone.split('-')[:-1]),
我知道这并不重要,但我想用蟒蛇的方式来做

提前谢谢

哦,如果区域为us-east1-b,则预期输出为


us-EAST 1

我认为使用rsplit就足够了:

或者简单地拆分即可:


好的,根据你的评论,我看到字符串切片将为你做这项工作。 阅读更多关于它的信息


试试这个-print us-east1-b[:-2]

你想删除最后两个字符吗?i、 e.你想要的是us-east1而不是us-east1-B根据我在区域名称中看到的,它们都是X-Y-Z。其中X是区域/大陆,如美国、欧盟、亚太地区。Y是位置北/东/南/西,Z是DC id。因此,基于此,我只想删除DC id。谢谢,但我不确定,DC id将始终为1个字符长。例如,如果一个区域是us-east-12,那么结果将是us-east,明白了,我的回答不会处理这种情况。需要更新它。谢谢
zone = 'us-east1-b'
print(zone.rsplit('-', 1)[0])
# us-east1
zone = 'us-east1-b'
lst = zone.split('-')
print("{}-{}".format(lst[0], lst[1]))
# us-east1