从python中的变量值中删除引号

时间:2014-11-26 22:12:28

标签: python

我从存储库中提取数据,服务器名称如下所示:

name=example%09

我需要替换%。我这样做:

name=re.sub("\%.*$","",name)

当我再次尝试使用name变量时,它在名称的乞讨处​​有双引号,如下所示:

打印名称,打印出来像这样:

"example

我如何摆脱"从python开始?

name="example%09"
name=re.sub("\%.*$","",name)
print Metric,int(time.time()),p.Val,"vSphereGuest="+name,"source=vSphereGuest","dc=dc1"

vSphereGuest.cpuUsageMhz 1417040919 0 vSphereGuest="example source=vSphereGuest dc=dc1

1 个答案:

答案 0 :(得分:2)

使用string.replace("%", "")

name = "example%09"
name = name.replace("%", "")

如果要在%之前获取字符串的值,请使用string.split("%"),然后获取所需的索引。

name = "example%09"
name = name.split("%")[0] #gets the 0 index of the list created when using `string.split()`

要替换字符串中可能出现的任何引号,您可以使用string.replace('"', '')

name = "example%09"
if '"' in name:
    name = name.replace('"', '')
相关问题