某處我是一個白癡,但我找不到在哪裏。Python:相同的字符串切片,兩個不同的結果
我正在通過ODBC使用PostgreSQL數據庫運行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]
非常感謝。
'Errorstring,則[openParenLocation + 1:closeParenLocation]'???你想通過使用'openParenLocation + 1:closeParenLocation'來做什麼? – alfasin
你最初是如何獲得/形成'errorString'?它看起來很像一個python元組的'repr'。也許這些數據是作爲一個元組來提供的,這將使得處理它變得毫無意義。對於你的問題,你正在爲'dbErrorString'指定'strippedString',但是在實際的切片中使用'errorString'而不是'strippedString'。 –
我想剝離外部左括號和右括號以及它們之外的所有內容。原始字符串是從ODBC返回的錯誤字符串,因爲在數據庫函數內遇到錯誤情況。該字符串不是來自Python並且不在元組中。 這項工作的目標是隻返回包含在這個長長的gobbledygook內的錯誤信息:「錯誤:收費不正確的狀態刪除」。 –