0

I'm trying to implement a Reed-Solomon encoder.

I start with a list of bytearray and then I have to convert all the elements of the list into str.

So now I have this list: ["bytearray(b'XXXXXXX')"]

But I have to retrieve the value from the list: "bytearray(b'XXXXXXX')" as a bytearray: bytearray(b'XXXXXXX')...

How can I perform this conversion?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103

1 Answers1

2

I don't think you're doing it right...

If you want to convert all list elements to str, you'd use the bytearray.decode method:

In [10]: lst = [bytearray(b'XXXXXXX')]

In [11]: newlst = [x.decode('ascii') for x in lst]

In [12]: newlst
Out[12]: ['XXXXXXX']

And the reverse of that is

In [13]: [bytearray(s, 'ascii') for s in newlst]
Out[13]: [bytearray(b'XXXXXXX')]
vaultah
  • 44,105
  • 12
  • 114
  • 143
  • Thanks for your answer ! I need to keep the "bitearray(b'XXXX') in the string i'm using. That's why i'm struggling turning it back to bitearray once i converted it to str – Farmer Joe Aug 27 '15 at 14:56
  • This is a strange requirement, but you can `eval` list elements: `[eval(x) for x in lst]` @FarmerJoe – vaultah Aug 27 '15 at 15:07
  • Oh thank you @vaultah , i didn't not know `eval`. I'm gonna check how this works ! – Farmer Joe Aug 27 '15 at 15:14
  • Though using `eval` is a bad practice http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice I think you've faced the [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem. – vaultah Aug 27 '15 at 15:16