Python无效语法错误 - > '< - calles as invalid

时间:2013-07-19 23:39:54

标签: python syntax python-3.x

我有一个程序正在制作并且我得到了最奇怪的错误......

我修复了所有这些内容,但现在它显示的语法无效:'

显示错误的声明是:

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()

1 个答案:

答案 0 :(得分:0)

如果hashs是您所说的整数,那么您应该if hashs == 1:,而不是'1''1'是一个字符串。

可能是您复制和粘贴代码的方式,但if语句看起来也是一个比它应该更多的空间。您应该决定选项卡约定,2个空格,4个空格等,并始终如一地使用它。

编辑:counter并且while循环是不必要的,并导致无限循环。

此代码适用于我:

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')

然后我的outfile包含:

222bc2522767626e27c64bb2b68a787f9e4758cd
f3ac7272e6d681c331580368e4b189445b9a9451
fdca95f9c68df6216af6d2eeb950a3344812bd62

编辑我正在使用python 2.7,因此您应该将输入从raw_input更改为input,并且您的print语句将正常运行。 Python 2.7只是认为我想打印一个元组。