0

So i have a tuple with this structure:

tuple =   ('Art School', 'Berlin', 'John Morgan(School1-Ge/Berlin);Andrew Martin (School1-IT/Roma); Tom Jones(School1-USA/Chicago)')

Each person structure it's like this :

John,Morgan(School name-Country-GE/City-Berlin)

I need to get every person email address and for this i'm using a function where i sent the name of the person as a parameter.

def get_email_address(name):
    if name is not '':
        try:
            sql = sql1%name
            cur.execute(sql)
        except Exception as e :
            print()
        txt = str(cur.fetchone())
        email = txt[2:len(txt)-3]
        return email
    return ''

where sql1="""select MAIL from email_table where NAME= '%s'"""

and NAME = tuple[2]

When there is only one person in tuple[2] is working fine ,but when the number increase i only get the last person email address.

My question is how to send all values from the tuple[2] to the email function to get all persons email address?

rzss275
  • 31
  • 5

2 Answers2

0

Try splitting tuple[2]:

tuple[2].split(';')

It will give output as:

['John Morgan(School1-Ge/Berlin)', 'Andrew Martin (School1-IT/Roma)', ' Tom Jones(School1-USA/Chicago)']

Now a for loop can be used to iterate trough the elements of this list.

Homer
  • 424
  • 3
  • 7
0

You should split the string to multiple names. I think you can split it on the character ";". The modified code will be something like follows:

tuple =   ('Art School', 'Berlin', 'John Morgan(School1-Ge/Berlin);Andrew Martin (School1-IT/Roma); Tom Jones(School1-USA/Chicago)')
names_combined = tuple[2]
names_split = names_combined.split(";")
for name in names_split:
    print(name)
    get_email_address(name)
gihan
  • 141
  • 5
  • I tried your solution and also i wanted to put the results in a list. I had 2 persons in tuple [2] but for one of them i didn't get an email because that person left and it's no longer in the table but the list looks like this : ['john_morgan@gmail.com', '', ''] and also when i have only 1 person that no longer works i get ['', ''] . So everytime i get this "," and i don't understand why. – rzss275 Sep 13 '20 at 17:42
  • I think you can use slicing, for each email, you can check the final character is "," email[-1]=="," and remove the final character by email = email[:,-1] – gihan Sep 13 '20 at 17:49
  • you can also refer to this, https://stackoverflow.com/questions/1038824/how-do-i-remove-a-substring-from-the-end-of-a-string-in-python – gihan Sep 13 '20 at 17:54