Filtering out certain bytes in python

后端 未结 4 528
鱼传尺愫
鱼传尺愫 2020-12-31 04:12

I\'m getting this error in my python program: ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters

This

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 05:02

    I think this is harsh/overkill and it seems painfully slow, but my program is still quick and after struggling to comprehend what was going wrong (even after I attempted to implement @John's cleaned_string implementation), I just adapted his answer to purge ASCII-unprintable using the following (Python 2.7):

    from curses import ascii
    def clean(text):
        return str(''.join(
                ascii.isprint(c) and c or '?' for c in text
                )) 
    

    I'm not sure what I did wrong with the better option, but I just wanted to move on...

提交回复
热议问题