Python cannot write variables to txt

as shown in the figure, I have printed out the rerz variable-> funny video self-media
but there is nothing in my txt file. But after I changed it to f.write ("helloworld") , the program ran normally and was successfully written in the txt file. Thank you very much for your advice.

Mar.10,2021

write to a file in binary

file=open('tmp.txt','wb')

convert encoding to gbk before writing a string

file.write(str.encode('gbk'))
< hr >

Update

use codecs module,

import codecs
content = ''
f = codecs.open('./1.txt','w','utf-8')
f.write(content)

start from the writing section of the main write file

try.except. The structure actually repeats the with as structure

try:
    f=open('test.txt')
except:
    print("error")
   
-sharp
with open('test.txt', mode='w') as f:

text files can be written directly. For example, txt,csv files can be specified in
format. It is normal to have garbled codes.

text writing cannot be written in binary and specified encoding

used the same method to test a wave of code, vsCode error, cheap charm normal
visual building main vsCode, if this is the case, I smile.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3badd-2c29a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3badd-2c29a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?