2010-12-16 52 views
1

嵌套循环我有以下代码:Python从文件

inputActionFile = '../action.txt' 
inputDaerahFile = '../daerah.txt' 
inputStuffFile = '../stuff.txt' 
inputTermsFile = '../terms.txt' 

outputFile = 'hasil.out' 

inputAction = open(inputActionFile, 'r') 
inputDaerah = open(inputDaerahFile, 'r') 
inputStuff = open(inputStuffFile, 'r') 
inputTerms = open(inputTermsFile, 'r') 

output = open(outputFile, 'w') 

for actionLine in inputAction: 
for daerahLine in inputDaerah: 
    for stuffLine in inputStuff: 
    for termsLine in inputTerms: 
    keyword = actionLine.strip() + ' ' + daerahLine.strip() + ' ' + stuffLine.strip() + ' ' + termsLine 
    output.write(keyword) 

inputAction.close() 
inputDaerah.close() 
inputStuff.close() 
inputTerms.close() 
output.close() 

我希望的结果,通过所有这些文件被循环嵌套,并逐一输出文件。但是,它只是迭代第四个循环。我在BaSH中做了类似的事情,并希望看到如何在Python中完成它。 bash的代码如下:

#!/bin/sh 
input1=$1 
input2=$2 
input3=$3 
input4=$4 
output=$5 

echo "###START###" > $output 
#old_IFS=$IFS 
IFS=' 
' # new field separator, EOL 

for line1 in `cat $input1`; 
do 
for line2 in `cat $input2`; 
do 
    for line3 in `cat $input3`; 
    do 
    for line4 in `cat $input4`; 
    do 
    echo $line1 $line2 $line3 $line4 >> $output; 
    done 
    done 
done 
done 

unset IFS; 
#IFS=$old_IFS 
+0

IMO它记下第一个文件的第一行X2 * X3 * X4(每个文件中的行数)次。这是你想要的吗?或者也许我错了,如果是的话请纠正我。 – 2010-12-16 08:42:46

回答

2

尝试:

inputAction = open(inputActionFile, 'r').readlines() 
inputDaerah = open(inputDaerahFile, 'r').readlines() 
inputStuff = open(inputStuffFile, 'r').readlines() 
inputTerms = open(inputTermsFile, 'r').readlines() 
+0

我的天啊。你们很快,很有帮助!我今天刚刚学过Python,看到在Python中,任务的花费是.52s,而在bash中的花费是14s,BaSH的for循环花费了1:48。这是用250000行创建文件。 Python岩石! – 2010-12-16 08:00:13

3

每个循环只会经历一次文件。在已经通过

for termsLine in inputTerms: 

一旦成功地循环,每次到那里的时候,它会跳过这个循环中,因为你已经达到了inputTerms文件的末尾。

您需要重新打开每个循环中的每个文件(或至少在其上搜索(0)),或者将文件读入内存中的列表。

所以,要么:

inputAction = open(inputActionFile, 'r').readlines() 
inputDaerah = open(inputDaerahFile, 'r').readlines() 
inputStuff = open(inputStuffFile, 'r').readlines() 
inputTerms = open(inputTermsFile, 'r').readlines() 

或者:

for actionLine in open(inputActionFile, 'r'): 
for daerahLine in open(inputDaerahFile, 'r'): 
    for stuffLine in open(inputStuffFile, 'r'): 
    for termsLine in open(inputTermsFile, 'r'): 
     etc.... 
0

这是你的Bash版本的一些变化,可能加速它(加上一些其他的变化)。

#!/bin/bash 
# you had sh, but your question tag says "bash" 
# if you really need Bourne shell portability, you should have tagged your 
# question "sh" and not "bash" 

input1=$1 
input2=$2 
input3=$3 
input4=$4 
output=$5 

echo "###START###" > $output 
#old_IFS=$IFS 

IFS=$'\n' # new field separator, EOL 

while read -r line1 
do 
    while read -r line2 
    do 
     while read -r line3 
     do 
      echo "$line1 $line2 $line3" | cat - "$input4" 
     done < "$input3" 
    done < "$input2" 
done < "$input1" >> "$output" 

通过消除内for循环,这可能是比你的版本快了很多,这取决于input4文件的大小。为最后保存文件写入可能会带来额外的速度优势。

你可以做while IFS=$'\n' read -r var,你就不需要保存和恢复的IFS值(如果必须这样做),但它在你在做的方式设置IFS一次可以节省一些重复的原创(我已经在我的修订中复制)。