List in a dictionary loop in Python

I have the following code:

    TYPES = {'hotmail':{'type':'hotmail', 'lookup':'mixed', 'dkim': 'no', 'signatures':['|S|Return-Path: postmaster@hotmail.com','|R|^Return-Path:\s*[^@]+@(?:hot|msn)','^Received: from .*hotmail.com$']},
             'gmail':{'type':'gmail', 'lookup':'mixed', 'dkim': 'yes', 'signatures':['|S|Subject: unsubscribe','','','']}
            }

    for type_key, type in TYPES.iteritems():
        for sub_type_key, sub_type in type.iteritems():
            for sig in sub_type['signatures']:
                if ("|S|" in sig):
                    #String based matching
                    clean_sig = sig[3:len(sig)]
                    if (clean_sig in file_contents):
                        sig_match += 1
                elif ("|R|" in sig):
                    clean_sig = sig[3:len(sig)]
                    #REGMATCH later
            if (sig_match == sig.count):
                return sub_type['type']

     return None

However, it generates an error:

for sig in sub_type['signatures']:
TypeError: string indices must be integers, not str

I assume that he will see that the list is pulled from a dictionary element, and allow me to iterate over it?

Python Newbie - Newbie :(

+3
source share
2 answers
for type_key, type in TYPES.iteritems():
    for sub_type_key, sub_type in type.iteritems():
        for sig in sub_type['signatures']:

it should be:

for type_key, type in TYPES.iteritems():
        for sig in type['signatures']:

But "type" is a poor choice of name in this case ... you do not want to obscure the embedded file.

Essentially, type_key has a name (hotmail or gmail), and type has a dictionary, which is the value associated with this key. So the type ['signatureatures] is what you want.

, "gmail" ; 'type_key' type['type'].

, , : (: )

providers = {
    'hotmail':{
        'type':'hotmail',
        'lookup':'mixed',
        'dkim': 'no',
        'signatures':[
            '|S|Return-Path: postmaster@hotmail.com',
            '|R|^Return-Path:\s*[^@]+@(?:hot|msn)',
            '^Received: from .*hotmail.com$']
    },
    'gmail':{
        'type':'gmail',
        'lookup':'mixed',
        'dkim': 'yes',
        'signatures':['|S|Subject: unsubscribe','','','']
    }
}

for provider, provider_info in providers.iteritems():
    for sig in provicer_info['signatures']:
        if ("|S|" in sig):
            #String based matching
            clean_sig = sig[3:len(sig)]
            if (clean_sig in file_contents):
                sig_match += 1
        elif ("|R|" in sig):
            clean_sig = sig[3:len(sig)]
            #REGMATCH later
    if (sig_match == sig.count):
        return provider

 return None
+7

[ , retracile , .]

:

TYPES = {
  'hotmail': {
    'type': 'hotmail',
    'lookup': 'mixed',
    'dkim': 'no', 
    'signatures': ['|S|Return-Path: postmaster@hotmail.com',
                   '|R|^Return-Path:\s*[^@]+@(?:hot|msn)',
                   '^Received: from .*hotmail.com$'],
  },
  'gmail': {
    'type': 'gmail',
    'lookup': 'mixed',
    'dkim': 'yes', 
    'signatures': ['|S|Subject: unsubscribe', '', '', ''],
  },
}

: dict, list tuple ( dicts — ), , , .

+3

Source: https://habr.com/ru/post/1723489/


All Articles