类型错误:需要一个类似字节的对象,不是“str",但类型是“字节" [英] TypeError: a bytes-like object is required, not 'str' but type is 'bytes'

查看:51
本文介绍了类型错误:需要一个类似字节的对象,不是“str",但类型是“字节"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图替换字符串中的一些字符,但 python 不能正确识别它的类型.任何想法为什么会这样??

So I'm trying to replace some chars in string but python doesn't recognize its type correctly. Any ideas why is that??

...
print(type(word))
word.replace('0', 'O')
...

<class 'bytes'> 

已打印但我得到:

TypeError: 需要一个类似字节的对象,而不是 'str'

TypeError: a bytes-like object is required, not 'str'

所以我正在对账单中已识别的文本进行一些文本更正.我在 self.text 变量中有一个可识别的文本,它有一个 .

So I'm making some text-correction over recognized text from a bill. I have a recognized text in the self.text variable which has a <str class>.

 def text_correction(self):
        '''
            Post processing, replace some characters.
        '''
        self.total = ""
        self.date = ""
        print(type(self.text))   #return <class 'str'> 

        lines = self.text.split('\n')
        new_string = ""

        for line in lines:

            line = line.encode("ascii")
            new_line = ""

            words = line.split()

            for word in words:

                type_of_word = self.get_type_of_word(word)
                print(type(word)) #return <class 'bytes'>
                if type_of_word == 0:
                    word.replace('0', 'O')
                    word.replace('l', 'I')
             ...

get_type_of_word 函数只是检查字符是大写/小写还是数字:

get_type_of_word function is just a check whether is upper/lower or digit a char is:

 def get_type_of_word(self, word):
        '''
            Define type of word.
        '''
        type_of_word = []
        count =0
        type_of_word.append(sum(1 for c in word if chr(c).isupper()))
        type_of_word.append(sum(1 for c in word if chr(c).islower()))
        type_of_word.append(sum(1 for c in word if chr(c).isdigit()))
        type_of_word.append(len(word) - sum(type_of_word))

        if type_of_word[0] == type_of_word[2] and type_of_word[0] != 0:
            return 2
        else:
            return type_of_word.index(max(type_of_word))

推荐答案

replace() 方法,当在 bytes 对象上使用时,也需要 bytes 对象作为参数.

The replace() method, when used on a bytes object, requires bytes objects as arguments too.

所以代替:

word.replace('0', 'O')

写:

word.replace(b'0', b'O')

但是,如果您进行文本处理,我想知道为什么要使用 bytes 对象而不是 str 对象.直接在字符串上工作更有意义.因此,请确保 word 的类型为 str 而不是 bytes 然后 word.replace('0', 'O') 会按预期工作.为此,您的代码只需要进行两次修改:

However, if you're text processing, I wonder why you work with bytes objects instead of str objects. Working directly on strings makes more sense then. So, make sure that word is of type str instead of bytes and then word.replace('0', 'O') would work as expected. To do this, your code only needs two modifications:

  • 删除以下语句:line = line.encode("ascii")
  • get_type_of_word() 中只需使用c 而不是chr(c)
  • remove the following statement: line = line.encode("ascii")
  • in get_type_of_word() just use c instead of chr(c)

还要注意 word.replace('0', 'O') 没有效果,因为它并没有真正改变单词而是返回它的(修改过的)副本.所以你应该分配它以产生任何效果,例如word = word.replace('0', 'O').

Also note that word.replace('0', 'O') has no effect, since it does not really change the word but returns a (modified) copy of it. So you should assign it to have any effect, e.g. word = word.replace('0', 'O').

这篇关于类型错误:需要一个类似字节的对象,不是“str",但类型是“字节"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆