问题描述
使用Python3.3
运行python脚本进行某些CAD文件转换,但出现错误
转换失败
回溯(最近一次调用):
文件Start.py,第141行,在convertLib中
lib.writeLibrary(modFile ,symFile)
在writeLibrary中的文件C:\ Python33 \Eagle2Kicad / Library \Library.py,第67行
self.writeSymFile(symFile)
文件C: \\ Python33 \Eagle2Kicad / Library \Library.py,第88行,在writeSymFile
devicepart.write(symFile)
文件C:\Python33\Eagle2Kicad / Common\Symbol.py ,第51行,写入
symbol.write(symFile)
文件C:\Python33\Eagle2Kicad / Common\Symbol.py,第114行,写入
symFile .write(pin.symRep())
文件C:\Python33\Lib\encodings\cp1252.py,第19行,在encode
中return codecs.charmap_encode(input,self .errors,encoding_table)[0]
UnicodeEncodeError:'charmap'编解码器不能在位置4编码字符'\x96':字符映射到< undefined>
似乎我的Windows7命令提示符中的首选编码是 NOT cp1252键入chcp显示活动代码页437)。
编辑:
strong>
由于python命令行的默认首选编码是cp1252,我试图从python命令行运行脚本而不是Windows命令提示符。但我仍然得到与上面相同的错误。任何人都可以建议?
解决方案写入文件时,不考虑控制台编码;只有区域设置。从:
在你的系统上,显然调用返回'cp1252'
。解决方法是总是明确地命名文件的编码; 传递编码
参数以设置所需的文件编码:
with open(filename,'w',encoding ='utf8')as symFile:
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?
解决方案 When writing to files, the console encoding is not taken into account; only the locale is. From the open()
function documenation:
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:
这篇关于更改Windows7的首选编码命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!