johnnyb
johnnyb

Reputation: 1825

pandas series convert to ascii from hex with error

I have a pandas.Series with multiple str values of hex like:

74 ,68 ,69 ,73 ,20 ,69 ,73 ,20 ,6d ,79 ,20 ,74 ,65 ,78 ,74 ,20 ,74 ,6f ,20 ,68 ,65 ,78
00, ff, aa, dd, 11, 22, 33, 44, 55, 00, ff, aa, dd, 11, 22, 33, 44, 55    
74 ,68 ,69 ,73 ,20 ,69 ,73 ,20 ,6d ,79 ,20 ,74 ,65 ,78 ,74 ,20 ,74 ,6f ,20 ,68 ,65 ,78
74 ,68 ,69 ,73 ,20 ,69 ,73 ,20 ,6d ,79 ,20 ,74 ,65 ,78 ,74 ,20 ,74 ,6f ,20 ,68 ,65 ,78
00, ff, aa, dd, 11, 22, 33, 44, 55, 00, ff, aa, dd, 11, 22, 33, 44, 55

I am trying to convert hex to ascii like:

df['value'] = df['value'].apply(lambda x: codec.decode(x, 'hex')

I am getting errors such as:

binascii.Error: decoding with 'hex' codec failed (Error: Odd-length string)    

I expect these errors on some lines, but need to change the values that can be converted to the converted state. How can I, on an error, do nothing with the value of the series?

Expected output:

this is my text to hex
00, ff, aa, dd, 11, 22, 33, 44, 55, 00, ff, aa, dd, 11, 22, 33, 44, 55
this is my text to hex
this is my text to hex
00, ff, aa, dd, 11, 22, 33, 44, 55, 00, ff, aa, dd, 11, 22, 33, 44, 55

Upvotes: 3

Views: 622

Answers (2)

Stephen Rauch
Stephen Rauch

Reputation: 49812

If you want to provide your own converter to allow it to respond to errors, you can provide a function instead of a lambda. Your requirements of how to handle the out of range values wasn't quite clear so I will just show a basic example:

def my_ascii_convert(char):
    value = int(char, 16)
    if 32 <= value < 128:
        return chr(value)
    else:
        return char

df['value'] = df['value'].apply(my_ascii_convert)

Upvotes: 4

Back2Basics
Back2Basics

Reputation: 7806

using int() is far simpler

def fn(foo):
    return int(foo, 16)

data = pd.Series(['74', '68', '69', '6d'])
data.apply(fn)

Upvotes: 1

Related Questions