我有一个.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
我需要创建两个列表,其中包含Implementation Authority.mail和Assigned Engineer.mail列下的值。当列具有compltete值(即没有空值)时,它可以正常工作。当列包含空值时,值会混合。
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
我尝试使用此代码,它完全适用于完整的列值。 谁能告诉我一个空值的解决方案?
答案 0 :(得分:0)
好像你没有分隔符。我为你的情况使用了多个空格。并填写空白。
试试这个:
#!/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']
答案 1 :(得分:-1)
你需要放置一个' null'或0作为占位符。
口译员将第二栏中的Guolin.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
然后在检查非空后附加值。
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])