本文介绍了pyodbc异常args有一些无法识别的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将pyodbc异常记录到日志文件中,问题是,使用标准的python日志记录,代码会产生另一个异常,该异常与编码出现在异常args中的一些未识别字符(看起来像中国人)有关,异常堆栈跟踪:

I am trying to log the pyodbc exception to a log file, the problem is, using the standard python logging, the code produces another exception related to encoding some unidentified characters (looks like Chines) that appears in the exception args, the exception stack trace:

---记录错误---追溯(最近一次通话最后一次):文件"\ Transfer \ transfer \ transfer_records.py",第123行,位于transfer_record中row.PUBSTAT,row.CITED)文件"\ Transfer \ transfer \ db_access.py",行476,在transfer_record中self.cursor.execute(sql_block,params)

--- Logging error --- Traceback (most recent call last): File "\Transfer\transfer\transfer_records.py", line 123, in transfer_record row.PUBSTAT, row.CITED) File "\Transfer\transfer\db_access.py", line 476, in transfer_record self.cursor.execute(sql_block, params)

pyodbc.IntegrityError :("23000","[23000][Oracle] [ODBC] [Ora] ORA-00001:唯一约束(REDWOOD.TITLE_PK)违反\ nORA-06512:在线53 \ n \ x00ઊȯ\ x00诔唱\ x00 \ x00㸸ੌȯ\ x00 \ x00 \ x00s \ x00 \ uec40ઉȯ\ x00 \ x00 \ uf600 \ x00 \ x00㸸ੌȯ\x00ꦓ倗翸\x00ᴘඇȯ\ x00㫏杦翸\x00ɘઊȯ\ x00텈ઉȯ\ x00㸸ੌȯ\ x00糆假设翸\ x00 \ x00 \ x00 \ x01 \ x00!\ x00 \ x00 \ x00H \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00촼杧翸\ x00(1)(SQLExecDirectW)')

pyodbc.IntegrityError: ('23000', '[23000] [Oracle][ODBC][Ora]ORA-00001: unique constraint (REDWOOD.TITLE_PK) violated\nORA-06512: at line 53\n\x00ઊȯ\x00诔唱\x00\x00㸸ੌȯ\x00\x00\x00s\x00\uec40ઉȯ\x00\x00\uf600\x00\x00㸸ੌȯ\x00ꦓ倗翸\x00ᴘඇȯ\x00㫏杦翸\x00ɘઊȯ\x00텈ઉȯ\x00㸸ੌȯ\x00糆倘翸\x00\x00\x00\x01\x00!\x00\x00\x00H\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00촼杧翸\x00 (1) (SQLExecDirectW)')

在处理上述异常期间,发生了另一个异常:

During handling of the above exception, another exception occurred:

回溯(最近通话最近):文件发射中的"C:\ Python \ lib \ logging__init __.py",行994stream.write(msg)文件"C:\ Python \ lib \ encodings \ cp1252.py",第19行,编码返回codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError:"charmap"编解码器无法对以下字符进行编码位置167-168:字符映射到调用堆栈:文件"/Transfer/trans_ui.py",第162行,在app.mainloop()在主循环中的文件"C:\ Python \ lib \ tkinter__init __.py",行1277self.tk.mainloop(n)在调用中的文件"C:\ Python \ lib \ tkinter__init __.py",第1699行返回self.func(* args)文件"/Transfer/trans_ui.py",行130,在transfer_records中start_transfer.main(options = options,app = self)文件"\ Transfer \ start_transfer.py",主行中的第19行transfer.start()文件"Transfer \ transfer \ transfer_records.py",第46行,在开始时list(map(self.transfer_record,self.transfer_rows))文件"Transfer \ transfer \ transfer_records.py",第145行,位于transfer_record中logging.error(异常详细信息:" + ex.args [1])消息:'异常详细信息:[23000] [Oracle] [ODBC] [Ora] ORA-00001:唯一约束(THOTH.TITLE_PK)已违反\ nORA-06512:在第53 \ n \ x00ઊȯ\ x00诔唱\ x00 \ x00㸸ੌȯ\ x00 \ x00 \ x00s \ x00 \ uec40ઉȯ\ x00 \ x00 \ uf600 \ x00 \ x00㸸ੌȯ\x00ꦓ倗翸\x00ᴘඇȯ\ x00㫏杦翸\x00ɘઊȯ\ x00텈ઉȯ\ x00㸸ੌȯ\ x00糆假设翸\ x00 \ x00 \ x00 \ x01 \ x00!\ x00 \ x00 \ x00H \ x00 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00촼杧翸\ x00(1)(SQLExecDirectW)的参数:()总时间:24.116901397705078

Traceback (most recent call last): File "C:\Python\lib\logging__init__.py", line 994, in emit stream.write(msg) File "C:\Python\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 167-168: character maps to Call stack: File "/Transfer/trans_ui.py", line 162, in app.mainloop() File "C:\Python\lib\tkinter__init__.py", line 1277, in mainloop self.tk.mainloop(n) File "C:\Python\lib\tkinter__init__.py", line 1699, in call return self.func(*args) File "/Transfer/trans_ui.py", line 130, in transfer_records start_transfer.main(options=options, app=self) File "\Transfer\start_transfer.py", line 19, in main transfer.start() File "Transfer\transfer\transfer_records.py", line 46, in start list(map(self.transfer_record, self.transfer_rows)) File "Transfer\transfer\transfer_records.py", line 145, in transfer_record logging.error("Exception details: " + ex.args[1]) Message: 'Exception details: [23000] [Oracle][ODBC][Ora]ORA-00001: unique constraint (THOTH.TITLE_PK) violated\nORA-06512: at line 53\n\x00ઊȯ\x00诔唱\x00\x00㸸ੌȯ\x00\x00\x00s\x00\uec40ઉȯ\x00\x00\uf600\x00\x00㸸ੌȯ\x00ꦓ倗翸\x00ᴘඇȯ\x00㫏杦翸\x00ɘઊȯ\x00텈ઉȯ\x00㸸ੌȯ\x00糆倘翸\x00\x00\x00\x01\x00!\x00\x00\x00H\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00촼杧翸\x00 (1) (SQLExecDirectW)' Arguments: () Total Time: 24.116901397705078

以退出代码0结束的过程

Process finished with exit code 0

产生异常的异常处理程序:

The Exception handler that produces the exception:

        except pyodbc.Error as ex:
            print("Exception: ", ex.args[1])
            self.database.rollback()
            self.records_counter['transfer_errors'] += 1
            logging.error("Exception details: " + ex.args[1])
            self.status_update("An Exception Occurred while transferring 
            record: " + row.CTRLNO + "-" + row.LEVL + " Details:" + 
            ex.args[1], msgbox='Error')

作为异常args的一部分出现的这些字符是什么?我如何摆脱它们,因为它们似乎正在产生编码异常

What are these characters that appears as part of the exception args? How I can get rid of them as it seems they are producing the encoding exception

推荐答案

您可以通过显式编码来摆脱它们,以便可以使用 errors =" replace" ignore 生成的字节不是很漂亮,但会被记录下来.

You can get rid of them by encoding explicitly so that you can use errors="replace" or ignore The resulting bytes will not be pretty but they will be logged.

logging.error("Exception: " + str(ex.args[1].encode(encoding='charmap', errors='replace')))

这篇关于pyodbc异常args有一些无法识别的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 21:54