在python中忽略空值列
我有一个.txt文件,其中有三列在python中忽略空值列,python,parsing,Python,Parsing,我有一个.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 Guol
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
我需要创建两个列表,其中包含列Implementation Authority.mail和Assigned Engineer.mail下的值。当列具有complte值(即没有空值)时,它可以完美地工作。当列包含空值时,值混合
aengg=[]
iauth=[]
with open('test.txt') as f:
for i, row in enumerate(f):
columns = row.split()
if len(columns) == 3:
aengg.append(columns[2])
iauth.append(columns[1])
print aengg
print iauth
我用这段代码试过了,它对完整的列值非常有效。
有谁能告诉我一个空值的解决方案吗?您需要放置一个“null”或0作为占位符 翻译会读国林。Pan@ell.com.cn在第二行中作为第二列 试试这个
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后追加值
with open('test.txt') as f:
for i, row in enumerate(f):
columns = row.split()
if len(columns) == 3:
if columns[2] != "null":
aengg.append(columns[2])
if columns[1] != "null":
iauth.append(columns[1])
你好像没有分离器。我为您的案例使用空格数。并用空白填充空白。 试试这个:
#!/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
这是输出
['id', 'ImplementationAuthority.email', 'AssignedEngineer.email']
['ALU02034116', 'bin.a.chen@shan.cn', 'bin.a.chen@ell.com.cn']
['ALU02035113', None, 'Guolin.Pan@ell.com.cn']
['ALU02034116', 'bin.a.chen@ming.com.cn', 'Guolin.Pan@ell.com.cn']
['ALU02022055', 'fria-sha-qdv@list.com', None]
['ALU02030797', 'fria-che-equipment-1@phoenix.com', 'Balagopal.Velusamy@phoenix.com']
['AssignedEngineer.email', 'bin.a.chen@ell.com.cn', 'Guolin.Pan@ell.com.cn', 'Guolin.Pan@ell.com.cn', None, 'Balagopal.Velusamy@phoenix.com']
['ImplementationAuthority.email', 'bin.a.chen@shan.cn', None, 'bin.a.chen@ming.com.cn', 'fria-sha-qdv@list.com', 'fria-che-equipment-1@phoenix.com']
首先让我们看看你到现在为止都做了什么?@qqvc我更新了问题。那么现在的输出是什么,预期的输出是什么。我不明白。我运行了您的示例,它似乎起了作用。您的数据文件是否使用多个空格或制表符来分隔列?如果是后者,
row.split(“\t”)
可能就是您所需要的。我建议在任何情况下都使用csv
模块。我认为(但我没有检查)它可以处理任意字符串作为列分隔符,因此如果您的文件使用多个空格,它应该能够适应。空值的定义是什么?例如,第3行和第5行都包含一个id和一个电子邮件地址,但需要有一种方法来确定电子邮件地址是指第二列还是第三列。我作为输入提供的文件是由工具自动生成的。这意味着它必须作为直接输入而不进行修改。如果他有一个大的.txt文件,他就不能这样做。间距有标准格式吗?在输出文件中是否有公共的空格数?不是每次都这样。@m170897017谢谢你的代码。但我真的不想附加一个none并将它们添加到列表中。我需要将适当的内容附加到列表中。@warrior因为您可以用任何需要的内容替换代码中的任何内容。代码不需要更改。这能解决你的问题吗?@m170897017我不想要那个空条目。如果我将其替换为任何内容,则替换的内容将出现在我不想要的列表中。我也试过你的代码,它显示“列表索引超出范围”@warriorpince那么你想用什么来代替无?一根绳子?还是?