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

在用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在列表中出现的第一次!所以可能会有副作用,不过对于一个单词或者短语还是没有问题的。

具体使用如下:

这里写图片描述

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

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值