Python:For循环不会完成

Ant*_*ras 1 python for-loop

在我开始之前,让我说我对编程很新,所以请不要杀了我.

作为练习,我编写了一个脚本,该脚本应该从txt中获取十六进制数列表,将它们转换为十进制并将它们写入另一个文件.这就是我想出的:

hexdata = open(raw_input("Sourcefile:")).read().split(',')
dec_data = []

print hexdata
x = -1
for i in hexdata:
    next_one = hexdata.pop(x+1)
    decimal = int(next_one, 16)
    print "Converting: ", next_one, "Converted:", decimal
    dec_data.append(decimal)

print dec_data

target = open(raw_input("Targetfile: "), 'w')
for n in dec_data:
    output = str(n)
    target.write(output)
    target.write(",")
Run Code Online (Sandbox Code Playgroud)

当我运行脚本时,它完成没有错误但是它只转换并写入源文件中的前30个数字并忽略所有其他数字,即使它们被加载到'hexdata'列表中.我尝试了几种变化,但它从不适用于所有数字(48).我究竟做错了什么?

Joh*_*Jr. 5

您的第一个循环是尝试迭代hexdata,同时使用hexdata.pop()将值从列表中拉出.只需将其更改为:

for next_one in hexdata:
    decimal = int(next_one, 16)
    print "Converting: ", next_one, "Converted:", decimal
    dec_data.append(decimal)
Run Code Online (Sandbox Code Playgroud)