Python 报错:ValueError: binary mode doesn‘t take an encoding argument

Python 报错:ValueError: binary mode doesn’t take an encoding argument

At that time, a json file was opened for writing information.
The format of the opened file was as follows

json_msg_out = open('json_msg_out', 'wb', encoding='utf-8')

Then write the information as follows

json.dump(str('string')+str(str(float))), json_msg_out, ensure_ascii=False)

Then when the program runs, it reports an error

ValueError: binary mode doesn't take an encoding argument

insert image description here
Then combined with the above picture, the problem can be clearly seen, because I opened the file in the "wb" format, that is, the file opened in the binary format, but I want to enter the string type, which is obviously wrong. Just change "wb" to "w" to solve this problem.

Guess you like

Origin blog.csdn.net/universe_R/article/details/121101131