“ValueError: embedded null character” when using open()

后端 未结 4 1176
一整个雨季
一整个雨季 2021-01-04 06:48

I am taking python at my college and I am stuck with my current assignment. We are supposed to take 2 files and compare them. I am simply trying to open the files so I can

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-04 07:32

    The problem is due to bytes data that needs to be decoded.

    When you insert a variable into the interpreter, it displays it's repr attribute whereas print() takes the str (which are the same in this scenario) and ignores all unprintable characters such as: \x00, \x01 and replaces them with something else.

    A solution is to "decode" file1_content (ignore bytes):

    file1_content = ''.join(x for x in file1_content if x.isprintable())
    

提交回复
热议问题