Python无效语法错误>'<调用为无效

2024-07-04 16:38:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,我正在做,我得到最奇怪的错误到处都是。。。在

我把它们都修好了,但现在它显示了无效语法:'

显示错误的语句是:

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

Tags: in程序forif错误line语法语句
1条回答
网友
1楼 · 发布于 2024-07-04 16:38:51

如果hashs是你说的整数,那么你应该有if hashs == 1:,而不是{}。'1'是一个字符串。在

这可能是您复制和粘贴代码的方式,但是if语句看起来也比它应该的向右多了一个空格。您应该决定一个制表符约定,2个空格,4个空格,等等,并始终如一地使用它。在

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

我的输入文件包含:

^{pr2}$

以下是我的终端输入和输出:

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我使用的是Python2.7,所以您应该将输入从raw_input改回input,这样print语句就可以正常工作了。Python2.7只是想打印一个元组。在

相关问题 更多 >

    热门问题