2

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]
8
  • for k, row in reader: only works (and unpacks) if the csv file has 2 colums. I'm not sure that it's what you mean. Commented Apr 10, 2018 at 19:20
  • because after that if u.lower() == row[0].lower(): compares with the first letter of row... misleading. Commented Apr 10, 2018 at 19:21
  • Why you are filling data at line 2? Commented Apr 10, 2018 at 19:29
  • After doing some research, I was lead to believe that I needed to construct the dimensions of the array prior to filling it which is what I was doing with that. Commented Apr 10, 2018 at 19:32
  • @Jean-FrançoisFabre The csv does have 2 columns, but that's not what I am wanting to base the array on. Basically, I'm needing to grab the items from the csv after matching it, and storing it in a new array along with data provided by the "tids" variable. Commented Apr 10, 2018 at 19:33

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

This is closer, but it doesn't iterate for each row in the CSV. It stops after the first item in the list.
@Justin make sure you return the 'data' after the first for loop
@Justin no problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.