I am writing some text to file in Python. The problem is that the string literals with extra singles quotes get written to the file. I want to avoid that.
I have a multiple language string which i first encode using
my_string = str('other language string').encode('utf-8')
file.open('my_file.txt', 'w', newline = '')
file.write(my_string)
It saves the following output to the file
b'other language string'
I want the output to be only
other language string
You've opened the file in text mode, which will encode data for you. Just write the string directly without encoding:
file.write('other language string')
The default encoding for text files is UTF-8, but you can give the open()
call a different encoding if you so wish.
Alternatively, open the file in binary mode with 'wb'
instead of 'w'
, encode manually like you did and write byte objects.
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.