Python如何删除字符串中不需要的子字符串
我的数据集中有这一列,其中包含以下数据样本:
player |
---|
David Johnson*+\JohnDa08 |
Kareem Hunt*\HuntKa00 |
Melvin Gordon\GordMe00 |
我正在尝试使用 Python 使其看起来像这样:
player |
---|
David Johnson |
Kareem Hunt |
梅尔文·戈登 |
请帮忙。
I have this column in my dataset with that contains the following data sample:
player |
---|
David Johnson*+\JohnDa08 |
Kareem Hunt*\HuntKa00 |
Melvin Gordon\GordMe00 |
and I'm trying to make it look like this using Python:
player |
---|
David Johnson |
Kareem Hunt |
Melvin Gordon |
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Python中,当你有一个
str
并且你想删除一个子字符串时,你可以使用.replace
方法:在你的情况下,最简单的事情是这样的:
或者,更确定要从字符串后面删除
00
:因为您的字符串并非全部以
00
结尾,但其他一些字符串以结尾>08
,我建议这样做:排除最后一个字符串中的两个字符
In Python when you have a
str
and you want to remove a substring you can use the.replace
method:In your case the most simple thing to do is this:
Or, to be more sure about the
00
to be removed from the back of the string:Since your strings are not all ending with
00
but some others are ending for example with08
, I would suggest this:which excludes the last two characters from the string
您可以在第一个特殊字符上
split
并获取第一个块:或者,使用
replace
:输出:
You can
split
on the first special character and get the first chunk:Or, using
replace
:Output: