简体   繁体   中英

TypeError: write() argument must be str, not dict (Python)

I am working on a project that displays my information in a txt file, it works perfectly fine with print but when I try to use write it gives me the following error:

TypeError: write() argument must be str, not dict

Here is my code:

for x in range(len(list)):
outfile = open('message.txt', 'w')
outfile.write(list[x])
outfile.close()

You can't write a dictionary to a string. Either use print(file=file_object) or file.write(str()) .

Code:

for x in range(len(input_list)):
    outfile = open('message.txt', 'w')
    print(input_list[x], file=outfile)
    outfile.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM