How to iterate over the file in python

前端 未结 5 1117
一向
一向 2020-11-29 04:49

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

5条回答
  •  野性不改
    2020-11-29 05:45

    with open('test.txt', 'r') as inf, open('test1.txt', 'w') as outf:
        for line in inf:
            line = line.strip()
            if line:
                try:
                    outf.write(str(int(line, 16)))
                    outf.write('\n')
                except ValueError:
                    print("Could not parse '{0}'".format(line))
    

提交回复
热议问题