Home > Software engineering >  Python ValueError: dictionary update sequence element #4 has length 3; 2 is required
Python ValueError: dictionary update sequence element #4 has length 3; 2 is required

Time:01-16

I need to transform a tuple to dictionary with its respective key->value. The issue is that for a specific tuple I get the following error:

ValueError: dictionary update sequence element #4 has length 3; 2 is required.

But for other tuples with the same format it transforms it without problems. Could someone guide me to what is the reason of the error?
In the attached code the tuple1 value works fine, but the tuple value gives the above error.

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)

CodePudding user response:

The problem is the fifth item in tupla:

'.1.3.6.1.4.1.35873.5.1.2.1.1.1.4="port=2"'

That line contains two equal signs, so the final .split('=') produces too many values.

CodePudding user response:

As noted by John Gordon, the data has an extraneous "=" in one of the rows.

I am not one hundred percent sure what you are hoping to achieve with your code, but I have a potential solution that might help to deal with the extraneous equals sign. The code may also be a bit easier to read:

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"']  

With Python, it is not necessary to use the length of the object and reference the index of the object (using [x]). We can simply parse the object directly with a for loop (for item in tupla):

miDiccionarioTupla = dict()
for item in tupla:           

We can split strings on a character (such as =) AND with this data, we can choose how many times to check for that using the maxsplit parameter.

    key, value = item.split('=', maxsplit=1)    

I am presuming you want to eliminate any extra quotes in the values to the right side of your items, so I added a .replace() method call on the value: (i.e. "CRITICAL" becomes CRITICAL). This replaces any examples of " with an empty string, essentially removing all the double quotes.

    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)
  •  Tags:  
  • Related