Justin
Justin

Reputation: 125

Python 3 array return value

fellow python programmers.

I have been working on a small tool that will help automate some email distribution for a repeated task.

I'm writing a function that takes a list of items and I'm stripping out the usernames in the email, matching it with a CSV file and finding the email that correlates with that user.

I am successfully getting all of the information that I need, however I'm trying to return the data in an array that is a list with 3 total columns that should look like so [reference#, user, email, reference#, user, email]

Below is the code that I have tried, but it just returns an array full of zeroes.

def gu(tids):
    data = [[0 for i in range(len(tids))] for j in range(1)]
    #In each ticket, splice out the username
    for tid in tids:
        #print(tid.Subject)
        su = tid.Body.find("from ") + 5
        eu = tid.Body.find(" has")
        u = tid.Body[su:eu]
        with open('c:\\software\\users_and_emails.csv', "r") as f:
            reader = csv.reader(f)
            for k, row in reader:
                if u.lower() == row[0].lower():
                    #print(row)
                    tidSubject = tid.Subject[30:-1]
                    data[k][0] = tidSubject
                    data[k][1] = row[0]
                    data[k][2] = row[1]
    return data

For whatever reason this returns an empty array of the appropriate length

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

Could someone help me out in understanding why it's not returning the appropriate value?

The below code without storing values in the array prints out the appropriate values.

def gu(tids):
    data = [[0 for i in range(len(tids))] for j in range(1)]
    #In each ticket, splice out the username
    for tid in tids:
        #print(tid.Subject)
        su = tid.Body.find("from ") + 5
        eu = tid.Body.find(" has")
        u = tid.Body[su:eu]
        with open('c:\\software\\users_and_emails.csv', "r") as f:
            reader = csv.reader(f)
            for row in reader:
                if u.lower() == row[0].lower():
                    #print(row)
                    tidSubject = tid.Subject[30:-1]
                    #data[i][0] = tidSubject
                    #data[i][1] = row[0]
                    #data[i][2] = row[1]
                    print(tidSubject)
                    print(row[0])
                    print(row[1])
                    #print(i)
        #return data

And it returns data similar to this (have to obscure actual returns, sorry)

47299
username1
 [email protected]
47303
username2
 [email protected]
47307
username3
 [email protected]
47312
username4
 [email protected]
47325
username5
 [email protected]

Upvotes: 2

Views: 4656

Answers (1)

Akshay Batra
Akshay Batra

Reputation: 137

Try this.

def gu(tids):
     data = []
    #In each ticket, splice out the username
    for tid in tids:
        #print(tid.Subject)
        su = tid.Body.find("from ") + 5
        eu = tid.Body.find(" has")
        u = tid.Body[su:eu]
        with open('c:\\software\\users_and_emails.csv', "r") as f:
            reader = csv.reader(f)
            for row in reader:
                if u.lower() == row[0].lower():
                    #print(row)
                    tidSubject = tid.Subject[30:-1]
                    subject = tidSubject
                    row0 = row[0]
                    row1 = row[1]
                    data.append([subject, row0, row1])
    return data

Upvotes: 3

Related Questions