简体   繁体   中英

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

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)

您可以在一行中完成此操作,从而返回已解码的元组列表。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM