I tried to write the following code and write the data to the file, but I got the following error...
How can I modify the code below to write text data to a file simply...? I would appreciate it if you could let me know.(By the way, my python was python2...)
Code
with open(text_file, 'w', encoding="utf-8") asfp:#Where the error occurs
fp.write(text)
Error
Traceback (most recent call last):
File "ExtractText.py", line 36, in <module>
with open(text_file, 'w', encoding="utf-8") asfp:
TypeError: 'encoding' is an invalid keyword argument for this function
By the way, if you erase the encoding argument, you get the following error...
Code
with open(text_file, 'w') asfp:# Where the error occurs
fp.write(text)
Error
Traceback (most recent call last):
File "ExtractText.py", line 37, in <module>
fp.write(text)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-10:ordinal not in range (128)
When writing, not when reading.
fp.write(text)
You may be experiencing an error in .
for.write(text.encode("utf-8")))
Can't you?
I wonder if the encoding of the platform is different.
If you try encoding="shift_jis", you may be able to do it
© 2025 OneMinuteCode. All rights reserved.