0

I'm using Python 3.7 to write a .csv file with the build in lib csv. The error occurs if input contains the Unicode character "White bullet" https://unicode-table.com/en/search/?q=%E2%97%A6:

Code:

with open(filename, 'w', newline='', encoding='ansi') as csvfile:
            filewriter = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)

filewriter.writerow(['H', debitor, None, customer_name, notes, setup.HQ])

Error: 'mbcs' codec can't encode characters in position 0--1: invalid character

Is this a bug or is there some limitation I'm not aware of?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Anders_K
  • 843
  • 7
  • 23

1 Answers1

1

The ANSI encoding (read about it in this question or on this wikipedia page) does NOT have the character "white bullet", so there is simply no way to represent that character with an ANSI encoding.

You have three option:

  1. Change the encoding to something that supports that specific char, for example utf-8 or some other encoding.
  2. Skip/remove that char.
  3. Replace that char with something else that is valid in ANSI encoding.
Ralf
  • 13,322
  • 4
  • 31
  • 55