[Numpy-discussion] A bug in loadtxt and how to convert a string array (hex data) to decimal?

Ryan May rmay31 at gmail.com
Thu Sep 18 17:39:47 EDT 2008


frank wang wrote:
> 
> 
> 
> Hi, All,
> 
> 
>  
> 
> 
> I have found a bug in the loadtxt function. Here is the example. The 
> file name is test.txt and contains:
> 
> 
> "Thist is test"
> 
> 
> 3FF 3fE
> 
> 
> 3Ef 3e8
> 
> 
> 3Df 3d9
> 
> 
> 3cF 3c7
> 
> 
>  
> 
> 
> In the Python 2.5.2, I type:
> 
> 
>  
> 
> 
> test=loadtxt('test.txt',comments='"',dtype='string',converters={0:lambda 
> s:int(s,16)})
> 
> 
>  
> 
> 
> test will contain
> 
> 
>  
> 
> 
> array([['102', '3fE'],
> 
> 
>        ['100', '3e8'],
> 
> 
>        ['991', '3d9'],
> 
> 
>        ['975', '3c7']],
> 
> 
>       dtype='|S3')
> 

It's because of how numpy handles strings arrays (which I admit I don't 
understand very well.)  Basically, it's converting the numbers properly, 
but truncating them to 3 characters.  Try this, which just forces it to 
expand to strings 4 characters wide:
					test=loadtxt('test.txt',comments='"',dtype='|S4',converters={0:lambda
	s:int(s,16)})

HTH,

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma



More information about the NumPy-Discussion mailing list