0

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

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @luz , is there a way to make it like ('2018.06.15', 'a') ('2018.06.15', 'b')
You mean not being inside a list? I've edited the answer.
0

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]

2 Comments

it throws an error AttributeError: 'int' object has no attribute 'decode'
You've got too many nested comprehensions, i is the individual bytes of the byte array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.