Domanda

I have the following code that gets a list of strings as a raw_input from the user:

def Lookup(Dict_list):
    print 'Please enter one alias per line. Leave blank when finished.'
    user_alias = [alias for alias in iter (raw_input, '') ]
    print user_alias
    for row in Dict_list:

        #print "row test"
        #print row
        if user_alias in row.values():
            print row


    for row in Dict_list:
        if user_alias[0] == row['_cn6ca']:
                email1 = row['_chk2m']
                print email1
                return email1
            if user_alias[1] == row['_cn6ca']:
                email2 = row['_chk2m']
                print email2
                return email2
            if user_alias[2] == row['_cn6ca']:
                email3 = row['_chk2m']
                print email3
                return email3

Lookup(Dict_list)

How can I alter the if user_alias == row['_cn6ca']: line so that it will look up and return an email for EACH of the user inputted user aliases? (These keys will stay the same)

My goal is to store these looked up emails to paste later in a .csv


Here is what is generated in the Terminal:

Please enter one alias per line. Leave blank when finished. PaulDu Jeanell Twanna JaneyD Charlot Janessa

['PaulDu', 'Jeanell', 'Twanna', 'JaneyD', 'Charlot', 'Janessa'] logout

[Process completed]

È stato utile?

Soluzione

I have stripped some details so that if the row (which is a dict type) has the user as one if its values, then we find their email id.

    Dict_list = [{'nokey':'someval'},{'_cn6ca':'Twanna', '_chk2m': "twanna@xyz"},{'_cn6ca':'Jeanell', '_chk2m': "jeanell@xyz"}]

    #print 'Please enter one alias per line. Leave blank when finished.'
    #user_alias = [alias for alias in iter (raw_input, '') ]
    user_alias = ['PaulDu', 'Jeanell', 'Twanna', 'JaneyD', 'Charlot','Janessa']
    print user_alias
    for user in user_alias:
        for row in Dict_list:    
            if user in row.values():
                print row

    for user in user_alias:
        for row in Dict_list:
            if row.has_key('_cn6ca') and row.has_key('_chk2m'):
                if user == row['_cn6ca']:
                    email = row['_chk2m']
                    print email

Output:

    ['PaulDu', 'Jeanell', 'Twanna', 'JaneyD', 'Charlot', 'Janessa']
    {'_cn6ca': 'Jeanell', '_chk2m': 'jeanell@xyz'}
    {'_cn6ca': 'Twanna', '_chk2m': 'twanna@xyz'}
    jeanell@xyz
    twanna@xyz
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top