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?

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.

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,
"""
commented: Good one. +1

picked something again tonyjv. Good one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.