我需要使用其各自的鍵->值將元組轉換為字典。問題是對于特定的元組,我收到以下錯誤:
ValueError:字典更新序列元素 #4 的長度為 3;2 是必需的。
但是對于具有相同格式的其他元組,它可以毫無問題地轉換它。有人可以指導我錯誤的原因是什么嗎?
在附加的代碼中 tuple1 值作業正常,但元組值給出了上述錯誤。
tupla = ['.1.3.6.1.4.1.35873.5.1.2.1.1.1.1="314"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.2="10943"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.3="RTU : otu-8000e-Comtec (172.17.74.133)..Alarm type: OPTICAL..Timestamp: Jan 15 2022 - 08:31..Severity: CLEAR..Link name: PROV-21-82-83-84 (PRI) RUTA 7 (PROV) - Port 2..Probable cause:"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.5="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.6="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.7="1"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.8="0x07e6010f081f1400"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.9="otu-8000e-Comtec (172.17.74.133)"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.1="PROV-21-82-83-84 (PRI) RUTA 7 (PROV)"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.2="0"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.3="0.18"', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.4=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.5=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.6=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.1=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.2=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.3=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.4=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.5=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.6=""', '.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.7=""']
tupla1 = ['.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.1.3701361="3701361"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.2.3701361="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.3.3701361="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.4.3701361="VALE-078-001"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.5.3701361="Microreflection Threshold 1 Violation"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.6.3701361="2021-09-02T19:14:04.834Z"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.7.3701361="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.8.3701361="1333972"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.9.3701361="http://SRVXPTPRODSTG01.vtr.cl/pathtrak/analysis/view.html#/node/1333972"', '.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.10.3701361="7"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="28400000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="HOLA"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="30800000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="7"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="40700000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="NONE"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="35600000"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="2"', '.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"']
miDiccionarioTupla= dict([(tupla[x].split('"')[0] tupla[x].split('"')[1]).split('=') for x in range(len(tupla))])
print(miDiccionarioTupla)
#miDiccionarioTupla1= dict([(tupla1[x].split('"')[0] tupla1[x].split('"')[1]).split('=') for x in range(len(tupla1))])
#print(miDiccionarioTupla1)
uj5u.com熱心網友回復:
問題是中的第五項tupla:
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"'
該行包含兩個等號,因此最終.split('=')產生的值太多。
uj5u.com熱心網友回復:
正如 John Gordon 所指出的,資料在其中一行中有一個無關的“=”。
我不是百分百確定你希望用你的代碼實作什么,但我有一個潛在的解決方案可能有助于處理無關的等號。代碼也可能更容易閱讀:
tupla = ['.1.3.6.1.4.1.35873.5.1.2.1.1.1.1="314"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.2="10943"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.3="RTU : otu-8000e-Comtec (172.17.74.133)..Alarm type: OPTICAL..Timestamp: Jan 15 2022 - 08:31..Severity: CLEAR..Link name: PROV-21-82-83-84 (PRI) RUTA 7 (PROV) - Port 2..Probable cause:"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.5="1"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.6="1"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.7="1"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.8="0x07e6010f081f1400"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.9="otu-8000e-Comtec (172.17.74.133)"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.1="PROV-21-82-83-84 (PRI) RUTA 7 (PROV)"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.2="0"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.3="0.18"',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.4=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.5=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.10.6=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.1=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.2=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.3=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.4=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.5=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.6=""',
'.1.3.6.1.4.1.35873.5.1.2.1.1.1.11.7=""']
tupla1 = ['.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.1.3701361="3701361"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.2.3701361="CRITICAL"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.3.3701361="CRITICAL"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.4.3701361="VALE-078-001"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.5.3701361="Microreflection Threshold 1 Violation"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.6.3701361="2021-09-02T19:14:04.834Z"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.7.3701361="0"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.8.3701361="1333972"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.9.3701361="http://SRVXPTPRODSTG01.vtr.cl/pathtrak/analysis/view.html#/node/1333972"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.101.1.10.3701361="7"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="28400000"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="HOLA"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="30800000"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="7"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="40700000"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="0"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="NONE"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.2.3701361.0="35600000"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.3.3701361.0="2"',
'.1.3.6.1.4.1.4100.2.2.1.2.1.102.1.4.3701361.0="CRITICAL"']
使用 Python,不需要使用物件的長度和參考物件的索引(使用[x])。我們可以簡單地使用 for 回圈 () 直接決議物件for item in tupla:
miDiccionarioTupla = dict()
for item in tupla:
我們可以在字符(例如 )上拆分字串,并且使用此資料,我們可以選擇使用引數=檢查多少次。maxsplit
key, value = item.split('=', maxsplit=1)
我假設您想消除專案右側值中的任何額外引號,因此我.replace()在值上添加了一個方法呼叫:(即"CRITICAL"變為CRITICAL)。"這用空字串替換了任何示例,基本上洗掉了所有雙引號。
value = value.replace('"', '')
miDiccionarioTupla.update({key: value})
print(miDiccionarioTupla)
miDiccionarioTupla1 = dict()
for item in tupla1:
key, value = item.split('=', 1)
value = value.replace('"', '')
miDiccionarioTupla1.update({key: value})
print(miDiccionarioTupla1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/412411.html
標籤:
