我正在收到错误消息,但我不确定该怎么办

时间:2019-05-22 15:20:09

标签: python python-3.x

this is what needs to be done

以下是列表data.txt中的一些值

79012400

adfadfaf

54523

29813360

30489059

30350069wqer

30530679

28863496

8787390
  

ValueError:int()以10为底的无效文字:'29933900jhgd'

def FileOpen(studentNumbers):
        count=0
        INFILE=open("data.txt","r")
        for line in INFILE:
            studentNumbers.append(line.rstrip())
            count+=1
        INFILE.close()
        return count


def AnalyseStudents(rawList,ValidNumbers,InvalidNumbers):
    num_sum=0
    for b in range(8,0,-1):
        num_sum += int(rawList[len(rawList)-7])**b


    result = num_sum%11

    if result == 0:
        VailidNumbers.append(rawList)
    else:
        InvalidNumbers.append(rawList)

def Write(outlist):
    OUTFILE=open("output.txt","W")
    for number in outlist:
        OUTFILE.write(number+"\n")
    OUTFILE.close()




inputList = []
outputList=[]
print(FileOpen(inputList),"number of lines read from file")
print("Analysing file")
AnalyseStudents(inputList,outputList,outputList)
print("Writing result to file..")
WriteFile(outputList)
print("Done.Please cheack the output file.")

2 个答案:

答案 0 :(得分:0)

按照添加的注释,看来您只需要前8个字符:

rawList = ['28698118adfs', 'abcdagsa', '2432142341432134', '30048117']
VailidNumbers = []
InvalidNumbers = []
length = 8
for rawString in rawList:
    if(rawString[0:length].isdigit()):
        num_sum = 0
        for b in range(length,0,-1):
            num_sum += int(rawString[length-b])**b
        print(num_sum)
        result = num_sum%11

        if result == 0:
            VailidNumbers.append(rawString)
        else:
            InvalidNumbers.append(rawString)
    else:
        InvalidNumbers.append(rawString)
print(InvalidNumbers)
print(VailidNumbers)

在您的30048117示例中,它扩展为(3 ^ 8 + 4 ^ 5 + 8 ^ 4 + 1 ^ 3 + 1 ^ 2 + 7 ^ 1)%11 = 8,不等于零,所以它将被添加到InvalidNumbers中。

但是,如果您的代码正确,则可以解决此问题:

rawList = ['28698118adfs', 'abcdagsa', '2432142341432134', '30048117']
VailidNumbers = []
InvalidNumbers = []
length = 8
for rawString in rawList:
    if(rawString[0:length].isdigit()):
        num_sum = 0
        number = rawString[0:length]
        for b in range(length,0,-1):
            num_sum += int(number[len(number) - 7]) ** b
        print(num_sum)
        result = num_sum%11

        if result == 0:
            VailidNumbers.append(rawString)
        else:
            InvalidNumbers.append(rawString)
    else:
        InvalidNumbers.append(rawString)
print(InvalidNumbers)
print(VailidNumbers)

答案 1 :(得分:0)

任何给定的数据样本都不符合您的逻辑-由于ValueErrornumber % 11 != 0导致的数据样本无效

编写演示数据:

with open("studs.txt","w") as f:
    f.write("""79012400 
adfadfaf
20570856 
54523 
29813360 
30489059 
30350069wqer 
30530679 
28863496 
8787390 """)

使用它来分析:

def AnalyseStudents(rawList):
    bad = []
    good = []
    for student in rawList:
        try:
            number = int(student[:8])
        except ValueError:  # not 8 numbers
            bad.append(student)
            continue        # next student

        v = 0
        for idx,n in enumerate(str(number)): # enumerate starts at idx 0 to 7
            n = int(n)
            idx = 8-idx  
            # detail printing # print(f"v ({v:>10}) += {n}*{idx} => {v+n*idx}")
            v += n*idx

        print(f"v ({v:>10}) % 11 = {v%11}")
        if v%11 == 0:
            good.append(student)
        else:
            # verification mishap
            bad.append(student)
    return good,bad  # return a tuple of 2 lists

处理演示数据文件:

with open("studs.txt") as f:
    students = [line.strip() for line in f if line.strip()]

print(f"{len(students)} lines read from file")
good, bad = AnalyseStudents(students)

with open("output.txt","w") as f:
    f.write( "\n".join(good))

if good:
    with open("output.txt","w") as f:
        f.write( "\n".join(good))
else:
    print("No good ones")

print(good)
print(bad)

输出:

10 lines read from file
v (       144) % 11 = 1
v (       121) % 11 = 0
v (       120) % 11 = 10
v (       165) % 11 = 0
v (       143) % 11 = 0
v (        88) % 11 = 0
v (       110) % 11 = 0
v (       198) % 11 = 0
v (       235) % 11 = 4
# good
['20570856', '29813360', '30489059', '30350069wqer', '30530679', '28863496']
# bad
['79012400', 'adfadfaf', '54523', '8787390']