SashaGreen
SashaGreen

Reputation: 193

Python string replace not working (bytes input expected?)

I am trying to use Pyner (https://github.com/dat/pyner) for NER. I give it a string of text to extract named entities from it. But I get an error. I am attaching the snipet where the error arises:

for s in ('\f', '\n', '\r', '\t', '\v'): #strip whitespaces
    text = text.replace(s, '')

Error message: {TypeError: a bytes-like object is required, not 'str'}

This error occurs even when I try multiple types of inputs (bytes objects)

text = b'This'
text = bytes("This".encode('utf-8'))

I think the problem is that replace is not getting the right input type. I am using python 3.5. What am I doing wrong? Please help!

Upvotes: 4

Views: 11981

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140256

replace works with str or bytes but not both mixed.

You could rewrite it like this:

for s in (b'\f', b'\n', b'\r', b'\t', b'\v'): #strip whitespaces except space!
    text = text.replace(s, b'')

you could also to apply strip which works with bytes type:

text = text.strip()  # remove all of the above + space

Also possible: convert back to str beforehand, trying:

text = str(text)

or

text = text.decode('utf-8')

(choose the best solution to avoid modifying a third-party package, as Patrick noted)

Upvotes: 8

Related Questions