2012-03-30 61 views
8

我需要线两个文件结合起来,在基础条件,在这些文件中的一个线是第二档的线的一部分。如何将两个文件中的行与python中的条件结合起来?

的第一个文件的一部分:

 
12319000 -64,7357668067227 -0,1111052148685535 
12319000 -79,68527661064425 -0,13231739777754026 
12319000 -94,69642857142858 -0,15117839559513543  
12319000 -109,59301470588237 -0,18277783185642743 
12319001 99,70264355742297 0,48329515727315125 
12319001 84,61113445378152 0,4060446341409862 
12319001 69,7032037815126 0,29803063228455073 
12319001 54,93886554621849 0,20958105041136763 
12319001 39,937394957983194 0,13623056582981297 
12319001 25,05574229691877 0,07748669438398018 
12319001 9,99716386554622 0,028110643107892755 

第二个文件的一部分:

 
12319000.abf mutant 1 
12319001.abf mutant 2 
12319002.abf mutant 3 

我需要创建一个文件,其中将生产线由这样的:从所有线路第一个文件和第二个文件的所有内容。第一列中的文件名除外。

正如你可以看到,有更多的,比第一文件中的一行,cooresponding在第二个行。我需要一个操作中,每个行来完成,所以输出应该是这样的:

 
12319000 -94,69642857142858 -0,15117839559513543 mutant 1 
12319000 -109,59301470588237 -0,18277783185642743 mutant 1 
12319001 99,70264355742297 0,48329515727315125 mutant 2 
12319001 84,61113445378152 0,4060446341409862 mutant 2 

我写这段代码:

oocytes = open(file_with_oocytes, 'r') 
results = open(os.path.join(path, 'results.csv'), 'r') 
results_new = open(os.path.join(path, 'results_with_oocytes.csv'), 'w') 
for line in results: 
    for lines in oocytes: 
     if lines[0:7] in line: 
      print line + lines[12:] 

但它打印出这一点,仅此而已,第一个文件中有45行:

 
12319000 99,4952380952381 0,3011778623990699 
    mutant 1 

12319000 99,4952380952381 0,3011778623990699 
    mutant 2 

12319000 99,4952380952381 0,3011778623990699 
    mutant 3 

代码有什么问题? 或者它应该以某种方式完全不同?

+7

+1包含您尝试的代码 – bernie 2012-03-30 21:26:30

+0

第一列的文件是否按顺序排列?可靠吗? – MattH 2012-03-30 21:32:14

+0

文件“小”吗?也就是说,他们可以一次读入并保存在记忆中吗? – 2012-03-30 21:33:47

回答

2

注意,该解决方案不依赖于任何领域,除了在第二文件的文件扩展名的长度的长度。

# make a dict keyed on the filename before the extension 
# with the other two fields as its value 
file2dict = dict((row[0][:-4], row[1:]) 
        for row in (line.split() for line in file2)) 

# then add to the end of each row 
# the values to it's first column 
output = [row + file2dict[row[0]] for row in (line.split() for line in file1)] 

仅用于测试目的,我用:

# I just use this to emulate a file object, as iterating over it yields lines 
# just use file1 = open(whatever_the_filename_is_for_this_data) 
# and the rest of the program is the same 
file1 = """12319000 -64,7357668067227 -0,1111052148685535 
12319000 -79,68527661064425 -0,13231739777754026 
12319000 -94,69642857142858 -0,15117839559513543 
12319000 -109,59301470588237 -0,18277783185642743 
12319001 99,70264355742297 0,48329515727315125 
12319001 84,61113445378152 0,4060446341409862 
12319001 69,7032037815126 0,29803063228455073 
12319001 54,93886554621849 0,20958105041136763 
12319001 39,937394957983194 0,13623056582981297 
12319001 25,05574229691877 0,07748669438398018 
12319001 9,99716386554622 0,028110643107892755""".splitlines() 

# again, use file2 = open(whatever_the_filename_is_for_this_data) 
# and the rest of the program will work the same 
file2 = """12319000.abf mutant 1 
12319001.abf mutant 2 
12319002.abf mutant 3""".splitlines() 

,你应该只使用普通的文件对象。测试数据的输出为:

[['12319000', '-64,7357668067227', '-0,1111052148685535', 'mutant', '1'], 
    ['12319000', '-79,68527661064425', '-0,13231739777754026', 'mutant', '1'], 
    ['12319000', '-94,69642857142858', '-0,15117839559513543', 'mutant', '1'], 
    ['12319000', '-109,59301470588237', '-0,18277783185642743', 'mutant', '1'], 
    ['12319001', '99,70264355742297', '0,48329515727315125', 'mutant', '2'], 
    ['12319001', '84,61113445378152', '0,4060446341409862', 'mutant', '2'], 
    ['12319001', '69,7032037815126', '0,29803063228455073', 'mutant', '2'], 
    ['12319001', '54,93886554621849', '0,20958105041136763', 'mutant', '2'], 
    ['12319001', '39,937394957983194', '0,13623056582981297', 'mutant', '2'], 
    ['12319001', '25,05574229691877', '0,07748669438398018', 'mutant', '2'], 
    ['12319001', '9,99716386554622', '0,028110643107892755', 'mutant', '2']] 
+0

我不完全理解,这应该如何与洞文件一起工作?我应该修改第一部分为 file1 = file1_old.splitlines() file2 = file2_old.splitlines() 然后执行第二部分? – Phlya 2012-03-30 21:41:14

+1

@Ilya我添加了几个注释,但基本上只是使用'fileX = open(filename)'而不是我对该文件的注释。 – agf 2012-03-30 21:44:50

+0

谢谢!现在就试试吧。 – Phlya 2012-03-30 21:51:38

6

Python中的文件句柄有状态;也就是说,他们不像列表那样工作。您可以反复遍历列表并每次获取所有值。另一方面,文件具有发生下一个read()的位置。当你遍历文件时,你每行都有read()。当到达最后一行时,文件指针位于文件的末尾。从文件末尾的read()返回字符串''

你需要做的在开始时oocytes文件一旦被读取,存储的值,也许这样的事情是什么:

oodict = {} 
for line in oocytes: 
    oodict[line[0:7]] = line[12:] 

for line in results: 
    results_key = line[0:7] 
    if results_key in oodict: 
     print oodict[results_key] + line 
1

好,简单的事情首先,你打印的换行符末行 - 你想放弃与线[0:0]

接下来,“行[0:7]”只测试线的前7个字符 - 你想考8个字符。这就是为什么用3个不同的突变值打印出“同一行”的相同值。

最后,你需要为结果中的每个行关闭并重新打开卵母细胞。如果不这样做,会在第一行结果后结束输出。

实际上,其他答案更好 - 不要为每一行结果打开和关闭卵母细胞 - 打开它并将其读入(到列表中)一次,然后遍历每行结果的列表。

+0

为什么要关闭并重新打开,当你可以寻求(0)? – 2012-03-30 21:42:09

相关问题