TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3

前端 未结 9 2234
Happy的楠姐
Happy的楠姐 2020-11-22 04:58

I\'ve very recently migrated to Py 3.5. This code was working properly in Python 2.7:

with open(fname, \'rb\') as f:
    lines = [x.strip() for x in f.readli         


        
9条回答
  •  佛祖请我去吃肉
    2020-11-22 05:16

    Use encode() function along with hardcoded String value given in a single quote.

    Ex:

    file.write(answers[i] + '\n'.encode())
    

    OR

    line.split(' +++$+++ '.encode())
    

提交回复
热议问题