0

Using Python3.3

Running a python script to do some CAD file conversion, but getting below error

Conversion Failed
Traceback (most recent call last):
File "Start.py", line 141, in convertLib
lib.writeLibrary(modFile,symFile)
File "C:\Python33\Eagle2Kicad/Library\Library.py", line 67, in writeLibrary
self.writeSymFile(symFile)
File "C:\Python33\Eagle2Kicad/Library\Library.py", line 88, in writeSymFile
devicepart.write(symFile)
File "C:\Python33\Eagle2Kicad/Common\Symbol.py", line 51, in write
symbol.write(symFile)
File "C:\Python33\Eagle2Kicad/Common\Symbol.py", line 114, in write
symFile.write(pin.symRep())
File "C:\Python33\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 '\x96' in position 4:     character maps to <undefined>

It seems the preferred encoding in my Windows7 command prompt is NOT cp1252 (typing chcp shows "Active code page 437"). How can I change it to cp1252?

Can anyone please suggest?

EDIT:

As the default preferred encoding of the python command line is cp1252, I tried running the script from python command line instead of the windows command prompt. But I am still getting the same error as above. Can anyone please suggest?

nad
  • 2,640
  • 11
  • 55
  • 96
  • You are *writing to a file*. The default encoding for files is [`locale.getpreferredencoding(False)`](https://docs.python.org/3/library/functions.html#open) and is *not* determined by your console settings; specify an explicit encoding when opening the file instead. – Martijn Pieters May 26 '14 at 18:49
  • Note that you normally would *show us the code* that triggered the exception; I have to make assumptions here that `symFile` in `symFile.write(pin.symRep())` is a file object opened with `open(filename, 'w')` or similar. – Martijn Pieters May 26 '14 at 18:50
  • You can use a cmd startup script that configures the console (`chcp`, `color`, `title`), sets environment variables, and loads doskey aliases. I use `"%APPDATA%\autorun.cmd"` and set a registry key to run this script when cmd starts: `reg add "HKCU\Software\Microsoft\Command Processor" /f /v AutoRun /t REG_EXPAND_SZ /d "\"^%APPDATA^%\autorun.cmd\""` – Eryk Sun May 27 '14 at 00:38
  • @MartijnPieters thanks. It worked after explicitly adding encoding type `open(modFileName,'w', encoding='utf-8').close()` `open(symFileName,'w', encoding='utf-8').close()` `modFile=open(modFileName,"a", encoding='utf-8')` `symFile=open(symFileName,"a", encoding='utf-8')` – nad May 27 '14 at 04:06

1 Answers1

0

When writing to files, the console encoding is not taken into account; only the locale is. From the open() function documenation:

In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding.

On your system, evidently that call returns 'cp1252'. The remedy is to always name the encoding for files explicitly; pass in an encoding argument to set the desired encoding for files:

with open(filename, 'w', encoding='utf8') as symFile:
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343