arrays - Python 3 数组返回值

标签 arrays python-3.x

各位Python程序员。

我一直在开发一个小工具,该工具将有助于自动执行重复任务的一些电子邮件分发。

我正在编写一个接受项目列表的函数,并删除电子邮件中的用户名,将其与 CSV 文件进行匹配,并查找与该用户相关的电子邮件。

我成功地获取了我需要的所有信息,但是我试图返回一个数组中的数据,该数组是一个总共 3 列的列表,应该如下所示 [引用#、用户、电子邮件、 引用#、用户、电子邮件]

下面是我尝试过的代码,但它只返回一个全是零的数组。

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

无论出于何种原因,这都会返回一个适当长度的空数组

[[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]]

有人可以帮助我理解为什么它没有返回适当的值吗?

下面的代码没有在数组中存储值,而是打印出适当的值。

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

它返回与此类似的数据(必须掩盖实际返回,抱歉)

47299
username1
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c5c3d5c2819dd5ddd1d9dcd1d4d4c2d5c3c3f0d6dfdf9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>
47303
username2
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1663657364243b737b777f7a77727264736565567079793875797b" rel="noreferrer noopener nofollow">[email protected]</a>
47307
username3
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d1d7c1d69789c1c9c5cdc8c5c0c0d6c1d7d7e4c2cbcb8ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>
47312
username4
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b7e786e793f266e666a62676a6f6f796e78784b6d646425686466" rel="noreferrer noopener nofollow">[email protected]</a>
47325
username5
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3d6d0c6d1968ec6cec2cacfc2c7c7d1c6d0d0e3c5cccc8dc0ccce" rel="noreferrer noopener nofollow">[email protected]</a>

最佳答案

试试这个。

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

关于arrays - Python 3 数组返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49761297/

相关文章:

c - 将数组结构传递给函数

python - (Python) 无法查看 Matplotlib 图

java - 牛顿拉夫森法 Java

python-3.x - 根据pytorch数据集中的文件名拆分数据集

python - 按名称动态地将参数传递给函数

arrays - Swift - 如何添加扩展嵌套字典?

python - 连接 3 维数组 python

python - 复制项目并根据机会将其添加到列表中

python - Spyder 快捷方式的目标文件是什么?

Python - 所有组合包括括号