[英]Remove Characters from string with replace not working
I have a number of strings from which I am aiming to remove charactars using replace. 我有许多字符串,我打算使用替换从这些字符串中删除字符。 However, this dosent seem to wake.
但是,这似乎似乎醒了。 To give a simplified example, this code:
举一个简化的例子,这段代码:
row = "b'James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38'"
row = row.replace("b'", "").replace("'", "").replace('b"', '').replace('"', '')
print(row.encode('ascii', errors='ignore'))
still ouputs this b'James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38'
wheras I would like it to output James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38
. 仍然输出这个
b'James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38'
我希望它输出James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38
。 How can I do this? 我怎样才能做到这一点?
Edit: Updataed the code with a better example. 编辑:使用一个更好的示例更新了代码。
You seem to be mistaking single quotes for double quotes. 您似乎将单引号误认为双引号。 Simple replace
'b
: 简单替换
'b
:
>>> row = "xyz'b"
>>> row.replace("'b", "")
'xyz'
As an alternative to str.replace
, you can simple slice the string to remove the unwanted leading and trailing characters: 作为
str.replace
的替代方法,您可以简单地对字符串进行切片以删除不需要的前导和尾随字符:
>>> row[2:-1]
'James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38'
In your first .replace
, change b'
to 'b
. 在您的第一个
.replace
,将b'
更改为'b
。 Hence your code should be: 因此,您的代码应为:
>>> row = "xyz'b"
>>> row = row.replace("'b", "").replace("'", "").replace('b"', '').replace('"', '')
# ^ changed here
>>> print(row.encode('ascii', errors='ignore'))
xyz
I am assuming rest of the conditions you have are the part of other task/matches that you didn't mentioned here. 我假设您所拥有的其余条件是您在此处未提到的其他任务/比赛的一部分。
If all you want is to take the string before first '
, then you may just do: 如果您只想将字符串放在第一个
'
之前,那么您可以这样做:
row.split("'")[0]
您尚未列出要删除的'b
:
.replace("'b", '')
import ast
row = "b'James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38'"
b_string = ast.literal_eval(row)
print(b_string)
u_string = b_string.decode('utf-8')
print(u_string)
out: 出:
b_string:b'James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38'
u_string: James Bray,/citations?user=8IqSrdIAAAAJ&hl=en&oe=ASCII,1985,6020,188.12,42,1.31,76,2.38
The real question is how to convert a string to python object. 真正的问题是如何将字符串转换为python对象。
You get a string which contains an a binary string, to convert it to python's binary string object, you should use eval()
. 您将获得一个包含二进制字符串的字符串,要将其转换为python的二进制字符串对象,应使用
eval()
。 ast.literal_eval()
is more safe way to do it. ast.literal_eval()
是更安全的方法。
Now you get a binary string, you can convert it to unicode string which do not start with "b" by using decode()
现在,您将获得一个二进制字符串,您可以使用
decode()
将其转换为不以“ b”开头的unicode字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.