-1

私は作成中のプログラムを持っていますが、いたるところで最も奇妙なエラーが発生しています...

それらをすべて修正しましたが、無効な構文が表示されるようになりました: '

エラーが表示されるステートメントは次のとおりです。

hashs <- Int

for i, line in enumerate(fp):

                if i == counter:

                    print(line)

                 if hashs == '1': <- error at the first '
                    line = line.encode('UTF-8')
                    hashc = hashlib.md5(line).hexdigest()

                if hashs == '2':
                    line = line.encode('UTF-8')
                    hashc = hashlib.sha1(line).hexdigest()
4

1 に答える 1

0

あなたhashsが言うように が整数の場合if hashs == 1:、 ではなくが必要'1'です。 '1'は文字列です。

コードをコピーして貼り付ける方法が原因である可能性がありますが、そのifステートメントは、必要以上に右側に 1 つのスペースがあるように見えます。タブ規則、2 つのスペース、4 つのスペースなどを決定し、それを一貫して使用する必要があります。

編集: ループは不要であり、無限ループになりましたcounterwhile

このコードは私のために働きます:

import hashlib

def main():
    hashs = 0

    read = str(raw_input('Please enter filename for input : '))
    output = str(raw_input('Please enter filename for output : ' ))
    hashs = int(raw_input('Select a Hash to convert to : '))

    if (output != ''):
        fileObj = open(output,"a")

    if (read != ''):
        numlines = 0
        for line in open(read):
            numlines +=1

        print ('Found ', numlines, ' lines to convert\n') 

        fp = open(read)

        for i, line in enumerate(fp):

            if hashs == 1:
                line = line.encode('UTF-8')
                hashc = hashlib.md5(line).hexdigest()

            if hashs == 2:
                line = line.encode('UTF-8')
                hashc = hashlib.sha1(line).hexdigest()

            if hashs == 3:
                line = line.encode('UTF-8')
                hashc = hashlib.sha224(line).hexdigest()

            if hashs == 4:
                line = line.encode('UTF-8')
                hashc = hashlib.sha256(line).hexdigest()

            if hashs == 5:
                line = line.encode('UTF-8')
                hashc = hashlib.sha384(line).hexdigest()

            if hashs == 6:
                line.encode('UTF-8')
                hashc = hashlib.sha512(line).hexdigest()

            fileObj.write(hashc)
            fileObj.write('\n')
main()

私の入力ファイルには以下が含まれています:

test file hash this yo
come on and hash, if you want to jam
mankind is to be surpassed

ここに私の端末の入力と出力があります:

Please enter filename for input : input
Please enter filename for output : outf 
Select a Hash to convert to : 2
('Found ', 3, ' lines to convert\n')

私のアウトファイルには次のものが含まれます:

222bc2522767626e27c64bb2b68a787f9e4758cd
f3ac7272e6d681c331580368e4b189445b9a9451
fdca95f9c68df6216af6d2eeb950a3344812bd62

edit私は python 2.7 を使用しているので、入力を からraw_inputに戻す必要がinputあります。print ステートメントは正しく機能します。Python 2.7 は、私がタプルを印刷したいと思っているだけです。

于 2013-07-19T23:48:37.450 に答える