Python 3.4字典输出不同于Python 2.7

Python 3.4字典输出不同于Python 2.7,python,python-2.7,dictionary,python-3.4,Python,Python 2.7,Dictionary,Python 3.4,我只是在学习Python,所以如果我完全遗漏了一些东西,我不会感到惊讶。这是一个BAM格式的序列文件,通过从pysam获取读取。在Python2.7和Python3.4中,所有内容都可以正常工作。这段代码 consensus = readDict[dictTag][6][maxCig][2:] print(consensus) 从python 2.7中输出此[b'GATC'],而不是所需的['GATC'] 这种行为的根源是什么?他们有什么解决办法吗 在Python2.x中,是一个字节字符串,u

我只是在学习Python,所以如果我完全遗漏了一些东西,我不会感到惊讶。这是一个BAM格式的序列文件,通过从pysam获取读取。在Python2.7和Python3.4中,所有内容都可以正常工作。这段代码

consensus = readDict[dictTag][6][maxCig][2:]
print(consensus)
从python 2.7中输出此
[b'GATC']
,而不是所需的
['GATC']


这种行为的根源是什么?他们有什么解决办法吗

在Python2.x中,是一个字节字符串,
u'GATC'
是一个Unicode字符串

在Python3.x中,
'GATC'
是一个Unicode字符串,
b'GATC'
是一个字节字符串

因此,在这两种情况下得到的结果(字节字符串)相同

您可以在Python 3.x中将字节字符串解码为Unicode,以获得所需的结果:

>>> s = b'GATC'
>>> s
b'GATC'
>>> s.decode() # default UTF-8 decoding.
'GATC'
>>> s.decode('ascii')
'GATC'
编辑 根据OP下面的评论,这里是另一个例子:

>>> s=[b'GATC',b'CTAG'] # A list of byte strings
>>> s
[b'GATC', b'CTAG']
>>> s = [t.decode() for t in s] # decoding the list to Unicode
>>> s
['GATC', 'CTAG']
>>> for t in s:    # iterating over the strings and characters
...  for c in t:
...   print(t,c)
...
GATC G
GATC A
GATC T
GATC C
CTAG C
CTAG T
CTAG A
CTAG G
如果跳过解码,会得到什么:

>>> s=[b'GATC',b'CTAG']
>>> for t in s:
...  for c in t:
...   print(t,c)
...
b'GATC' 71    # displays as byte strings and byte values (71 == ASCII 'G', etc.)
b'GATC' 65
b'GATC' 84
b'GATC' 67
b'CTAG' 67
b'CTAG' 84
b'CTAG' 65
b'CTAG' 71

在Python2.x中,
'GATC'
是字节字符串,
u'GATC'
是Unicode字符串

在Python3.x中,
'GATC'
是一个Unicode字符串,
b'GATC'
是一个字节字符串

因此,在这两种情况下得到的结果(字节字符串)相同

您可以在Python 3.x中将字节字符串解码为Unicode,以获得所需的结果:

>>> s = b'GATC'
>>> s
b'GATC'
>>> s.decode() # default UTF-8 decoding.
'GATC'
>>> s.decode('ascii')
'GATC'
编辑 根据OP下面的评论,这里是另一个例子:

>>> s=[b'GATC',b'CTAG'] # A list of byte strings
>>> s
[b'GATC', b'CTAG']
>>> s = [t.decode() for t in s] # decoding the list to Unicode
>>> s
['GATC', 'CTAG']
>>> for t in s:    # iterating over the strings and characters
...  for c in t:
...   print(t,c)
...
GATC G
GATC A
GATC T
GATC C
CTAG C
CTAG T
CTAG A
CTAG G
如果跳过解码,会得到什么:

>>> s=[b'GATC',b'CTAG']
>>> for t in s:
...  for c in t:
...   print(t,c)
...
b'GATC' 71    # displays as byte strings and byte values (71 == ASCII 'G', etc.)
b'GATC' 65
b'GATC' 84
b'GATC' 67
b'CTAG' 67
b'CTAG' 84
b'CTAG' 65
b'CTAG' 71

在Python2.x中,
'GATC'
是字节字符串,
u'GATC'
是Unicode字符串

在Python3.x中,
'GATC'
是一个Unicode字符串,
b'GATC'
是一个字节字符串

因此,在这两种情况下得到的结果(字节字符串)相同

您可以在Python 3.x中将字节字符串解码为Unicode,以获得所需的结果:

>>> s = b'GATC'
>>> s
b'GATC'
>>> s.decode() # default UTF-8 decoding.
'GATC'
>>> s.decode('ascii')
'GATC'
编辑 根据OP下面的评论,这里是另一个例子:

>>> s=[b'GATC',b'CTAG'] # A list of byte strings
>>> s
[b'GATC', b'CTAG']
>>> s = [t.decode() for t in s] # decoding the list to Unicode
>>> s
['GATC', 'CTAG']
>>> for t in s:    # iterating over the strings and characters
...  for c in t:
...   print(t,c)
...
GATC G
GATC A
GATC T
GATC C
CTAG C
CTAG T
CTAG A
CTAG G
如果跳过解码,会得到什么:

>>> s=[b'GATC',b'CTAG']
>>> for t in s:
...  for c in t:
...   print(t,c)
...
b'GATC' 71    # displays as byte strings and byte values (71 == ASCII 'G', etc.)
b'GATC' 65
b'GATC' 84
b'GATC' 67
b'CTAG' 67
b'CTAG' 84
b'CTAG' 65
b'CTAG' 71

在Python2.x中,
'GATC'
是字节字符串,
u'GATC'
是Unicode字符串

在Python3.x中,
'GATC'
是一个Unicode字符串,
b'GATC'
是一个字节字符串

因此,在这两种情况下得到的结果(字节字符串)相同

您可以在Python 3.x中将字节字符串解码为Unicode,以获得所需的结果:

>>> s = b'GATC'
>>> s
b'GATC'
>>> s.decode() # default UTF-8 decoding.
'GATC'
>>> s.decode('ascii')
'GATC'
编辑 根据OP下面的评论,这里是另一个例子:

>>> s=[b'GATC',b'CTAG'] # A list of byte strings
>>> s
[b'GATC', b'CTAG']
>>> s = [t.decode() for t in s] # decoding the list to Unicode
>>> s
['GATC', 'CTAG']
>>> for t in s:    # iterating over the strings and characters
...  for c in t:
...   print(t,c)
...
GATC G
GATC A
GATC T
GATC C
CTAG C
CTAG T
CTAG A
CTAG G
如果跳过解码,会得到什么:

>>> s=[b'GATC',b'CTAG']
>>> for t in s:
...  for c in t:
...   print(t,c)
...
b'GATC' 71    # displays as byte strings and byte values (71 == ASCII 'G', etc.)
b'GATC' 65
b'GATC' 84
b'GATC' 67
b'CTAG' 67
b'CTAG' 84
b'CTAG' 65
b'CTAG' 71


请注意,这种行为上的差异是无害的。如果打印这些字符串,“b”和“u”将消失。我正试图使用该字符串并在范围内为j执行a(len(一致意见),以便一次遍历每个字符串的一个位置。如果我能理解如何实现,我喜欢解码的想法。@whereswalden:在Python 3中不是。打印bytestring会导致
repr()
正在编写。如果要将其打印为文本,您需要将其解码为Unicode。谢谢,您为我指明了正确的方向。我能够使用此y=[Unicode(I)for I in consensus]为了得到我所需要的。解码可能更好,但我一直得到一个没有属性解码错误的列表。我正在学习。事实上,我的解决方案在python 3.4中不起作用,所以我仍然运气不佳。看起来我现在将继续使用python 2.7。请注意,这种行为上的差异是无害的。如果打印这些字符串,“b”和“u”的disappear。我正试图获取该字符串,并对范围内的j进行一次计算(len(一致意见),以一次遍历每个字符串的一个位置。如果我能理解如何实现它,我喜欢解码的想法。@whereswalden:在Python 3中不是。打印bytestring会导致
repr()
正在编写。如果要将其打印为文本,您需要将其解码为Unicode。谢谢,您为我指明了正确的方向。我能够使用此y=[Unicode(I)for I in consensus]为了得到我所需要的。解码可能更好,但我一直得到一个没有属性解码错误的列表。我正在学习。事实上,我的解决方案在python 3.4中不起作用,所以我仍然运气不佳。看起来我现在将继续使用python 2.7。请注意,这种行为上的差异是无害的。如果打印这些字符串,“b”和“u”的disappear。我正试图获取该字符串,并对范围内的j进行一次计算(len(一致意见),以一次遍历每个字符串的一个位置。如果我能理解如何实现它,我喜欢解码的想法。@whereswalden:在Python 3中不是。打印bytestring会导致
repr()
正在编写。如果要将其打印为文本,您需要将其解码为Unicode。谢谢,您为我指明了正确的方向。我能够使用此y=[Unicode(I)for I in consensus]为了得到我所需要的。解码可能更好,但我一直得到一个没有属性解码错误的列表。我正在学习。事实上,我的解决方案在python 3.4中不起作用,所以我仍然运气不佳。看起来我现在将继续使用python 2.7。请注意,这种行为上的差异是无害的。如果打印这些字符串,“b”和“u”的disappear。我正试图获取该字符串,并对范围内的j进行一次计算(len(一致意见),以一次遍历每个字符串的一个位置。如果我能理解如何实现它,我喜欢解码的想法。@whereswalden:在Python 3中不是。打印bytestring会导致
repr()
正在编写。如果要将其打印为文本,您需要将其解码为Unicode。谢谢,您为我指明了正确的方向。我能够使用此y=[Unicode(I)for I in consensus]为了得到我所需要的。解码可能会更好,但我一直得到一个列表没有属性解码错误。我正在学习。实际上,我的解决方案在python 3.4中不起作用,所以我仍然运气不佳。看起来我现在将坚持使用python 2.7。如果您
打印(共识[0])
(共识列表中的第一项),您不会注意到错误“b”不再出现,因为它只表示字符串的类型。如果您
打印(一致[0])
(一致列表中的第一项),您将不再注意到“b”,因为它只表示