nyi
nyi

Reputation: 3229

byte literal array to string

I am new to Python, I am calling an external service and printing the data which is basically byte literal array.

results = q.sync('([] string 2#.z.d; `a`b)')

print(results)
[(b'2018.06.15', b'a') (b'2018.06.15', b'b')]

To Display it without the b, I am looping through the elements and decoding the elements but it messes up the whole structure.

for x in results:
    for y in x:
        print(y.decode())

2018.06.15
a
2018.06.15
b

Is there a way to covert the full byte literal array to string array (either of the following) or do I need to write a concatenate function to stitch it back?

('2018.06.15', 'a') ('2018.06.15', 'b')  
(2018.06.15,a) (2018.06.15,b)

something like the following (though I want to avoid this approach )

for x in results:
    s=""
    for y in x:
        s+="," +y.decode()
    print(s)

,2018.06.15,a
,2018.06.15,b

Upvotes: 0

Views: 177

Answers (2)

luz.arthur
luz.arthur

Reputation: 135

Following the previous answer, your command should be as follows: This code will result in a list of tuples.

[tuple(x.decode() for x in item) for item in result]

The following code will return tuples:

for item in result:
     t = ()
     for x in item:
             t = t + (x.decode(),)     
     print(t)

Upvotes: 1

Bubble Bubble Bubble Gut
Bubble Bubble Bubble Gut

Reputation: 3358

You can do it in one line, which gives you back a list of decoded tuples.

[tuple(i.decode() for i in y) for x in result for y in x]

Upvotes: 0

Related Questions