0
data = data2 = ""

with open('file2.txt', encoding='utf-8', mode='r') as fp:
    data = fp.read()

with open('file2.txt', encoding='utf-8', mode='r') as fp:
    data2 = fp.read()

data += "\n"
data += data2

with open('file3.txt', 'w') as fp:
    fp.write(data)

So I saw this code in another question similar like this, and tried using it. But it didn't work, it kept giving me error like this:

Traceback (most recent call last):
line 45, in <module>
    fp.write(data)
line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to <undefined>

Can someone explain me why it is not working and how to fix it?

Teriyaki
  • 29
  • 4
  • The output file in the first example is using a default encoding that isn't UTF-8. A good habit is to *always* declare the encoding when reading or writing files. "Explicit is better than implicit." -- *The Zen of Python* – Mark Tolonen Jul 05 '22 at 05:54
  • If you provide your input files we would be able to help better since this is a problem with the encoding of your input files and cannot be reproduced with files that have normal chars in them (which is why it worked for the other question but not for you). Also, you have opened the same file `file2.txt` twice in your code. – M B Jul 05 '22 at 05:58
  • I just ran your code, the logic works fine. I suspect it has something to do with the files and the characters in there. Can update with the txt files? – user3667054 Jul 05 '22 at 06:23
  • Welcome to Stack Overflow. Hint: where the code says `with open('file2.txt', encoding='utf-8', mode='r') as fp:`, what do you think the `encoding='utf-8'` part means? Do you see why something similar might also be necessary for the output file? – Karl Knechtel Jul 05 '22 at 06:33

0 Answers0