python处理数据——去除字符串两端的引号

来源:互联网 发布:微信运动的数据来源 编辑:程序博客网 时间:2024/05/16 08:04

在用python处理数据,会出现获得的数据本身两端带有引号,而我们需要的是形如xxx,而不是“xxx”否则就会出现问题。比如:
这里写图片描述

『解决方法一:』
使用lstrip()和rsrtip()字符串函数

函数说明如下:

str.lstrip([chars])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

>>> '   spacious   '.lstrip()'spacious   '>>> 'www.example.com'.lstrip('cmowz.')'example.com'

str.rstrip([chars])
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:

>>> '   spacious   '.rstrip()'   spacious'>>> 'mississippi'.rstrip('ipz')'mississ'

具体使用如下:
这里写图片描述

『解决方法二』
先把字符转换为列表,使用列表的remove函数,再把列表拼成字符串

函数说明如下:

array.remove(x)
Remove the first occurrence of x from the array.

但是remove(x)每次只能只能移除x在列表中出现的第一次!所以可能会有副作用,不过对于一个单词或者短语还是没有问题的。

具体使用如下:

这里写图片描述

总结:可能还有更多的方法(比如转换成列表可以定位后删除),以上两种方法应该可以应对较多情况。

1 0
原创粉丝点击