Before I start let me just say that I’m really new to programming so please don’t kill me.
As an exercise I wrote a script that is supposed to take a list of hex numbers from a txt, convert them to decimal and write them to another file. This is what I came up with:
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(",")
When I run the script it finishes whithout errors however it only converts and writes the first 30 numbers from my sourcefile and ignores all the other ones, even though they are loaded into the ‘hexdata’ list. I’ve tried several variations but it never works with all the numbers (48). What am I doing wrong?
Your first loop is trying to iterate over hexdata, while at the same time pulling values out of the list using hexdata.pop(). Just change it to something like: