I have a text file with some hexadecimal numbers and i am trying to convert it to decimal. I could successfully convert it, but it seems before the loop exist it reads some unwanted character and so i am getting the following error.
Traceback (most recent call last):
File "convert.py", line 7, in <module>
print >>g, int(x.rstrip(),16)
ValueError: invalid literal for int() with base 16: ''
My code is as follows
f=open('test.txt','r')
g=open('test1.txt','w')
#for line in enumerate(f):
while True:
x=f.readline()
if x is None: break
print >>g, int(x.rstrip(),16)
Each hexadecimal number comes in a new line for input
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…