某处我是个白痴,但我找不到。
我正在使用PostgreSQL数据库通过ODBC运行Python脚本。我试图从数据库异常消息中提取有意义的片段。这是原始消息,添加了换行符以便于阅读:
(-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -2147467259), None)
请注意,此字符串中有两组括号。首先,我找到外部的位置并将它们切掉。这给出了预期的结果:
-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -2147467259), None
然后,我尽可能地告诉相同的代码,我剥去了另一组括号和它们之外的所有内容。这给出了这个结果:
(0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -214746725
开括号仍然在这里,即使我以相同的方式使用find()方法的结果,两次都在开括号位置添加一个作为切片的开头。
以下是代码:
print (errorString)
openParenLocation = errorString.find('(')
closeParenLocation = errorString.rfind(')')
strippedString = errorString[openParenLocation + 1:closeParenLocation]
openParenLocation = strippedString.find('(')
closeParenLocation = strippedString.rfind(')')
dbErrorString = errorString[openParenLocation + 1:closeParenLocation]
print (strippedString)
print ("{}, {}".format(openParenLocation, closeParenLocation))
print (dbErrorString)
这是原始输出,没有添加换行符:
(-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers', 'ERROR: Charge not in a correct status to delete;\nError while executing the query', None, 0, -2147467259), None)
-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers', 'ERROR: Charge not in a correct status to delete;\nError while executing the query', None, 0, -2147467259), None
36, 191
(0, 'Microsoft OLE DB Provider for ODBC Drivers', 'ERROR: Charge not in a correct status to delete;\nError while executing the query', None, 0, -214746725
使用更小的字符串测试代码按预期工作:
testString = "(abc(def)ghij)"
openParenLocation = testString.find('(')
closeParenLocation = testString.rfind(')')
strippedTestString = testString[openParenLocation + 1:closeParenLocation]
openParenLocation = strippedTestString.find('(')
closeParenLocation = strippedTestString.rfind(')')
finalTestString = strippedTestString[openParenLocation + 1:closeParenLocation]
非常感谢。
答案 0 :(得分:1)
看起来像这一行:
dbErrorString = errorString[openParenLocation + 1:closeParenLocation]
应改为:
dbErrorString = strippedString[openParenLocation + 1:closeParenLocation]
答案 1 :(得分:0)
鉴于您的字符串看起来像Python语法,您是否考虑过使用标准ast
库模块为您完成所有这些工作?
>>> errorString =r"""\
(-2147352567, 'Exception occurred.', (0, 'Microsoft OLE DB Provider for ODBC Drivers',
'ERROR: Charge not in a correct status to delete;\nError while executing the query',
None, 0, -2147467259), None)"""
>>> import ast
>>> a = ast.parse(errorString).body[0].value
>>> a
<_ast.Tuple at 0x10802d3d0>
>>> a.elts[0]
<_ast.Num at 0x10802d410>
>>> a.elts[0].n
-2147352567
>>> a.elts[1]
<_ast.Str at 0x10802d450>
>>> a.elts[1].s
'Exception occurred.'
>>> a.elts[2]
<_ast.Tuple at 0x10802d490>
>>> # so now lather/rinse repeat: iterate over a.elts[2].elts
>>> a.elts[3]
<_ast.Name at 0x10802d650>
>>> a.elts[3].id
'None'
更简单的方法是使用ast.literal_eval
将字符串直接转换为它描述的Python对象。它就像内置eval
,但从安全角度来看是安全的,因为它不会评估任何非文字内容(因此,任何恶意errorString
内容都不会被执行)。
>>> a = ast.literal_eval(errorString)
>>> a[0]
-2147352567
>>> a[1]
'Exception occurred.'
>>> a[2][0]
0
等