忽略python中的空值列

2024-05-14 20:40:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个.txt文件,其中有三列。在

id      ImplementationAuthority.email   AssignedEngineer.email
ALU02034116     bin.a.chen@shan.cn bin.a.chen@ell.com.cn
ALU02035113                                     Guolin.Pan@ell.com.cn
ALU02034116     bin.a.chen@ming.com.cn Guolin.Pan@ell.com.cn
ALU02022055     fria-sha-qdv@list.com
ALU02030797     fria-che-equipment-1@phoenix.com    Balagopal.Velusamy@phoenix.com

我需要创建两个由列实现下的值组成的列表权威.mail分配工程师.mail. 当列有compltete值(即没有空值)时,它可以完美地工作。当列包含空值时,这些值混合在一起。在

^{pr2}$

我用这段代码尝试过,它对完整的列值非常有效。 有谁能告诉我一个空值的解决方案吗?在


Tags: 文件txtcombinemailmailcnpan
2条回答

您需要将“null”或0作为占位符。在

翻译会读郭林。潘@易趣网在第二行作为第二列。在

试试这个

id      ImplementationAuthority.email   AssignedEngineer.email
ALU02034116     bin.a.chen@shan.cn bin.a.chen@ell.com.cn
ALU02035113     null                   Guolin.Pan@ell.com.cn
ALU02034116     bin.a.chen@ming.com.cn Guolin.Pan@ell.com.cn
ALU02022055     fria-sha-qdv@list.com  null
ALU02030797     fria-che-equipment-1@phoenix.com    Balagopal.Velusamy@phoenix.com

然后在检查not null后追加值。在

^{pr2}$

你好像没有分离器。我为你的案子用了很多空格。并用空白填充空白。在

试试这个:

#!/usr/bin/env python
# -*- coding:utf-8 -*- 

aengg = []
iauth = []

with open('C:\\temp\\test.txt') as f:
    for i, row in enumerate(f):
        columns = row.split()
        if len(columns) == 2:
            # when there are more than 17 spaces between two elements, I consider it as a third element in the row, then I add a None between them
            if row.index(columns[1]) > 17:
                columns.insert(1, None)
            # if there are less than 17 spaces between two elements, I consider it as the second element in the row, then I add a None to the tail
            else:
                columns.append(None)
        print columns
        aengg.append(columns[2])
        iauth.append(columns[1])

print aengg
print iauth

这是输出。在

^{pr2}$

相关问题 更多 >

    热门问题