【发布时间】:2012-05-03 21:26:51
【问题描述】:
在我开始之前,让我先说我真的是编程新手,所以请不要杀了我。
作为练习,我编写了一个脚本,该脚本应该从 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(",")
当我运行脚本时,它会在没有错误的情况下完成,但它只会转换并写入我的源文件中的前 30 个数字并忽略所有其他数字,即使它们已加载到“hexdata”列表中。我尝试了几种变体,但它从不适用于所有数字(48)。我做错了什么?
【问题讨论】:
-
hexdata.pop(x+1)好像很奇怪...