Replace non-ASCII characters with a single space

后端 未结 7 1435
死守一世寂寞
死守一世寂寞 2020-11-22 16:17

I need to replace all non-ASCII (\\x00-\\x7F) characters with a space. I\'m surprised that this is not dead-easy in Python, unless I\'m missing something. The following func

相关标签:
7条回答
  • 2020-11-22 16:49

    As a native and efficient approach, you don't need to use ord or any loop over the characters. Just encode with ascii and ignore the errors.

    The following will just remove the non-ascii characters:

    new_string = old_string.encode('ascii',errors='ignore')
    

    Now if you want to replace the deleted characters just do the following:

    final_string = new_string + b' ' * (len(old_string) - len(new_string))
    
    0 讨论(0)
提交回复
热议问题