Hi All,

I have a list of hex values that originated from a text string that was downloaded having now created a list of thes values approx 804 of them, when i run the list items through a loop to conert hex into dec had an error looking at this i noticed in the data were new line commands i.e. '\n'.
I have tried to remove these with the remove command but with no luck.
These may appear in different places within the list depending on the qty of data so they will appear irregularly.
Any Ideas on how to remove these items from my list

Regards

Deepthought

Try splitting the original on the newline and working with the resulting list.
split_str=orig_string.split("\n")

OP, I'm guesssing you have something like this (maybe more than 4 letters per item):

list_of_hex = ['12AF', 'B3D9', 'A5CC', '6EA1\n', '298F', 'A005\n', ...]

You want a new list of ints, but can't convert the items with the trailing '\n'.

You could do several things, but here's a quick and dirty one:

for index, item in enumerate(list_of_hex):
    if '\n' in item: # If the item has a trailing '\n'
        list_of_hex[index] = item[:-1] # The last item is '\n' and not just '\'

# Now you have a list of hex values without the trailing '\n'
list_of_ints = [int(hex_value, 16) for hex_value in list_of_hex] # and that's it

Hope this helps.

You might use something like this:

list_hex1 = ['12AF', 'B3D9', 'A5CC', '6EA1\n', '298F', 'A005\n']

# strips off white spaces
list_hex2 = [item.strip() for item in list_hex1]

print(list_hex1)
print(list_hex2)

'''result -->
['12AF', 'B3D9', 'A5CC', '6EA1\n', '298F', 'A005\n']
['12AF', 'B3D9', 'A5CC', '6EA1', '298F', 'A005']
'''

That is awesome Lardmeister. I didn't know strip() does that.
In that case the entire code be written as:

    list_of_ints = [int(hex_value.strip(), 16) for hex_value in list_of_hex]

Hi Guys,
Thanks for all the answers. As I tried to say earlier but was not very eloquent with was that the data I was recieving should had been in a HEX format as 782 bytes long however for some reason I could not understand the string was littered with eronious characters (out of the HEX range expected) ie \n zz ys etc.
After spending a lot of time searching and reading I found that the string i used was a "regular string" and that from the udp i needed to pass the data to the string in a particular format.
The line below shows how i passed the udp byte to the string:-

 import binascii
 mystring = binascii.hexlify(data)

This resolved the problems and i got the data passed from udp into mystring with no eronious characters.

Thanks again for your help hope this thread helps others in the future.

DeepThought

This question has already been answered. Start a new discussion instead.