2014-12-19 76 views
0

我有一个.txt文件,其中有三列。忽略python中的空值列

id  ImplementationAuthority.email AssignedEngineer.email 
ALU02034116  [email protected] [email protected] 
ALU02035113          [email protected] 
ALU02034116  [email protected] [email protected] 
ALU02022055  [email protected] 
ALU02030797  [email protected] [email protected] 

我需要创建两个列表,其中包括执行Authority.mail和Assigned Engineer.mail列下的值。当列具有复合值时(即没有空值),它可以很好地工作。当列包含空值时,这些值会混合在一起。

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 

我试过用这段代码,它完全适用于完整的列值。 任何人都可以请告诉我一个解决方案的空值?

+1

请首先告诉我们您到目前为止尝试过什么? – GLHF 2014-12-19 06:01:29

+0

@qqvc我更新了这个问题。 – WarriorPrince 2014-12-19 06:06:44

+0

那么现在的输出是什么,以及预期的输出是什么。不能理解。我运行你的例子,它似乎工作。 – Marcin 2014-12-19 06:14:29

回答

-1

您需要放置'null'或0作为占位符。

翻译员将在第二行读取[email protected]作为第二栏。

试试这个

id  ImplementationAuthority.email AssignedEngineer.email 
ALU02034116  [email protected] [email protected] 
ALU02035113  null     [email protected] 
ALU02034116  [email protected] [email protected] 
ALU02022055  [email protected] null 
ALU02030797  [email protected] [email protected] 

,然后检查不为空后追加值。

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]) 
+0

我输入的文件是由工具自动生成的。这意味着它必须作为直接输入给出而不修改。 – WarriorPrince 2014-12-19 06:22:12

+1

如果他有一个大的.txt文件,他不能这样做。 – GLHF 2014-12-19 06:23:43

+0

间距是否有标准格式?它在输出文件中有共同数量的空格吗? – 2014-12-19 06:23:57

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', '[email protected]', '[email protected]'] 
['ALU02035113', None, '[email protected]'] 
['ALU02034116', '[email protected]', '[email protected]'] 
['ALU02022055', '[email protected]', None] 
['ALU02030797', '[email protected]', '[email protected]'] 
['AssignedEngineer.email', '[email protected]', '[email protected]', '[email protected]', None, '[email protected]'] 
['ImplementationAuthority.email', '[email protected]', None, '[email protected]', '[email protected]', '[email protected]'] 
+0

你认为..它不适合每次。 – GLHF 2014-12-19 06:32:38

+0

@ m170897017感谢您的代码。但我真的不想追加一个没有,并将它们添加到列表中。我需要将适当的内容附加到列表中。 – WarriorPrince 2014-12-19 10:01:16

+0

@WarriorPrince您可以将代码中的None替换为您想要的任何内容。代码不需要改变。这是否能解决您的问题? – 2014-12-19 12:15:46