0

UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>

with open('F:\Temp\python\matches-details.csv', 'w') as output_file:
        dict_writer = csv.DictWriter(output_file, keys)
        dict_writer.writeheader()
        dict_writer.writerows(matches_details)
        print("file created")

I edited the code to be:

with open('F:\Temp\python\matches-details.csv', 'w',encoding='utf-8) as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(matches_details)``
    print("file created")

but the text in my csv file become as this: (Ø§Ù„ÙØ±ÙŠÙ‚ الأول)

  • Hello welcome to Stackoverflow. Checkout the [tour] and [ask]. You're going to have a little more context in your question. With what you currently have given, it difficult to infer what your issues is. – Marcelo Paco Mar 22 '23 at 22:23
  • Not related to the error, but remember to use a raw string when it contains literal backslash characters. – Barmar Mar 22 '23 at 22:26
  • You could try opening it with an encoding, like: `with open('F:\Temp\python\matches-details.csv', 'w', encoding='utf-8') as output_file:` – egeres Mar 22 '23 at 22:28

1 Answers1

0

Try adding encoding='utf-8' when opening it:

with open('F:\Temp\python\matches-details.csv', 'w', encoding='utf-8') as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(matches_details)
    print("file created")
ahmedg
  • 309
  • 1
  • 2
  • 12