0

Hi,

I have this cvs file comprising of hex values. But my objective is to have them all converted to normal base 10 Dec format.

myfile.csv

0x0000004d,0x00005275,0x37106800,0x000008a2,0x000009a2
0x0000004d,0x00005275,0x370d4e00,0x000008a2,0x000009a2
0x0000004d,0x00005275,0x37106800,0x000008a2,0x000009a2
0x0000004d,0x00005275,0x370d4e00,0x000008a2,0x000009a2

I tried

for line in open('myfile.csv'):
                for word in line:
                       word = int(word,16)

word = int(word,16)
ValueError: invalid literal for int() with base 16: 'x'

How can I get around this?

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by e-papa
0

Python comes with a csv module you should use to extract the data.

Search your documentation for how to write to files. The way you are doing it is totally wrong.

Edited by lrh9: n/a

1
for line in open('myfile.csv'):
     for word in line.split(','):
         # 0 tells that integer has base style Python style
         print "%s," % int(word, 0),
     print
""" Output:
77, 21109, 923822080, 2210, 2466,
77, 21109, 923618816, 2210, 2466,
77, 21109, 923822080, 2210, 2466,
77, 21109, 923618816, 2210, 2466,
"""

Edited by pyTony: n/a

Votes + Comments
Good one.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.