Python line.split(':')len是2但仍然是Value Error

时间:2014-11-01 19:18:57

标签: python email split

我遇到了一个我无法解决的问题。很简单,我有2个文本文件,一个是hashes_found,格式为

uncrackedHash:crackedHash

和第二个文件email_list的格式为:

email:uncrackedHash

现在我要做的是打印出电子邮件并破解散列,如果未破解的散列是相同的。

我不断得到的错误是:

email, uncracked = i.split(':')
ValueError: too many values to unpack

然而len(i)= 2

另外,我知道我的打开文件是非常规和不好的做法,但它不应该影响代码。这就是我2年前学习Python的方式,一旦我学会了正确的方法,我就会进入这个大会。

hashes_found = open('hashes_found.txt', 'r')
email_list = open('email_list.txt', 'r')

for i in email_list:
    i = i.strip()
    email, uncracked = i.split(':')

    for j in hashes_found:
        j = j.strip()

        try:
            uncracked_2, cracked = j.split(':')
        except Exception,e:
            print e
            continue

        if uncracked == uncracked_2:
            print email, ':', cracked
        else:
            continue

2 个答案:

答案 0 :(得分:1)

你说len(i)是2,但这与len(i.split(':'))不一样......

我的猜测:你有空行,并使用DOS格式(所以行是\r\n)。

要跳过空行:

for i in email_list:
    i = i.strip()

    if i == '':
        continue

该行也可能没有:,您可以跳过以下内容:

for i in email_list:
    i = i.strip()

    if i == '':
        continue

    try:
        email, uncracked = i.split(':')
    except ValueError:
        print("Warning: this line has no `:'\n  %s" % i, file=sys.stderr)
        # Or python2:
        # print >> sys.stderr, "Warning: this line has no `:'\n  %s" % i
        continue

答案 1 :(得分:0)

您可以使用辅助变量:

    hashes_found = open('hashes_found.txt', 'r')
    email_list = open('email_list.txt', 'r')

    for i in email_list:
        aux = i.strip('\r\n').split(':')

        email= aux[0]
        uncracked = aux[1]