简体   繁体   中英

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 . 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 :

>>> 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:

>>> 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 . 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.

You get a string which contains an a binary string, to convert it to python's binary string object, you should use eval() . ast.literal_eval() is more safe way to do it.

Now you get a binary string, you can convert it to unicode string which do not start with "b" by using decode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM