1

我有以下代码:

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
4

3 回答 3

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....
于 2010-12-16T08:25:55.740 回答
2

尝试:

inputAction = open(inputActionFile, 'r').readlines()
inputDaerah = open(inputDaerahFile, 'r').readlines()
inputStuff = open(inputStuffFile, 'r').readlines()
inputTerms = open(inputTermsFile, 'r').readlines()
于 2010-12-16T07:50:23.700 回答
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以你在原始文件中所做的方式设置一次来节省一些重复(我已经复制了在我的修订版中)。

于 2010-12-16T15:49:03.843 回答