简体   繁体   English

字节文字数组到字符串

[英]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. 我是Python的新手,我在调用外部服务并打印基本上是字节文字数组的数据。

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. 为了不使用b显示它,我正在遍历元素并解码元素,但是它弄乱了整个结构。

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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