I have to write playing card symbols (Spades, Hearts, Diamonds, Clubs) into a text file. I know their unicode values (2660, 2665, 2666, 2663) and the print command displays them correctly on the screen, but write to file gives an error.
I’ve written a small piece of code to demonstrate the problem:
OutFile = “Mod.txt”
Out = open(OutFile,“w”)
Spades = ‘\u2660’
Line = “Spades= {}”.format(Spades)
print(Spades)
print(Line)
Out.write(Line)
Out.close()
Write to screen is ok:
![]()
Spades= ![]()
But write to file gives an error:
Traceback (most recent call last):
File “C:\Users\tmind\Development\Python\Probeersels\utf.py”, line 9, in
Out.write(Line)
File “C:\Users\tmind\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py”, line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u2660’ in position 8: character maps to
Any help will be appreciated. Regards, Tom