Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python使用多个文件中的值注释输入文件_Python - Fatal编程技术网

如何使用python使用多个文件中的值注释输入文件

如何使用python使用多个文件中的值注释输入文件,python,Python,我有400个制表符分隔的文本文件,每个文件中有600万行。以下是文件的格式: ### input.txt col1 col2 col3 col4 col5 ID1 str4 123 cond1 1 ID1 str1 234 cond1 0 ID1 str2 567 cond1 0 ID1 str3 789 cond1 1 ### file1.txt col1

我有400个制表符分隔的文本文件,每个文件中有600万行。以下是文件的格式:

 ### input.txt 
col1    col2    col3    col4    col5
ID1     str4    123     cond1   1
ID1     str1    234     cond1   0
ID1     str2    567     cond1   0
ID1     str3    789     cond1   1


### file1.txt
col1    col2    col3    col4    col5
ID2     str1    235     cond1   0
ID2     str2    567     cond2   3
ID2     str3    789     cond1   3
ID2     str4    123     cond2   0

### file2.txt
col1    col2    col3    col4    col5
ID3     str1    235     cond1   0
ID3     str2    567     cond2   4
ID3     str3    789     cond1   1

file3.txt, file4.txt.....
我需要使用以下条件将文件file1..filen中column1中的值添加到input.txt文件中的column6:

1. columns 2 and 3 as key 
2. If the key is found in files1...filen and if column5>=2 add the value   
from col1 to col6 in the input file.
所需的输出是使用第1列中的值对input.txt文件中的每一行进行注释 如果找到密钥,则从file1..filen

Output.txt
col1    col2    col3    col4    col5    col6
ID1     str4    123    cond1    1    
ID1     str1    234    cond1    0    
ID1     str2    567    cond1    0       ID2,ID3,
ID1     str3    789    cond1    1       ID2,  

我有一个awk解决方案。但是,问题是代码非常慢,因为它必须在400个文件中迭代input.txt中的每个键,每个文件中有600万行,即400*6mi=24亿行。这需要几个小时到几天的时间。是否有人可以通过减少迭代时间和提高处理时间来推荐一个更好的python解决方案。

我建议您使用DB,在其中加载数据,然后运行所需的更新。我的第一个快速想法是:听起来好像您从Input.txt中获取了一个键,并通过所有文件中的所有行进行比较。因此,如果它在第2行中只出现一次,则可以不扫描其他6mio。如果从fileN.txt中获取该行并将其与Input.txt进行比较,则只能运行,直到找到键,然后才能继续执行fileN中的下一行。取决于数据,这可能需要相当长的时间。我建议您使用DB,在其中加载数据,然后运行所需的更新。我的第一个快速想法是:听起来好像您从Input.txt中获取了一个键,并通过所有文件中的所有行对其进行比较。因此,如果它在第2行中只出现一次,则可以不扫描其他6mio。如果从fileN.txt中获取该行并将其与Input.txt进行比较,则只能运行,直到找到键,然后才能继续执行fileN中的下一行。取决于数据,这可能需要相当长的时间。