Python 3.4 dictionary output different from python 2.7 -
Python 3.4 dictionary output different from python 2.7 -
i learning python not surprised if missing here. bam formatted sequencing file read in fetch pysam. works in python 2.7 in python 3.4 code
consensus = readdict[dicttag][6][maxcig][2:] print(consensus)
outputs [b'gatc']
instead of needed ['gatc']
python 2.7
what root of behavior , solutions?
in python 2.x 'gatc'
byte string , u'gatc'
unicode string.
in python 3.x 'gatc'
unicode string , b'gatc'
byte string.
so getting same result (byte string) in both cases.
you can decode byte string unicode in python 3.x result want:
>>> s = b'gatc' >>> s b'gatc' >>> s.decode() # default utf-8 decoding. 'gatc' >>> s.decode('ascii') 'gatc'
edit per op's comments below, here's example:
>>> s=[b'gatc',b'ctag'] # list of byte strings >>> s [b'gatc', b'ctag'] >>> s = [t.decode() t in s] # decoding list unicode >>> s ['gatc', 'ctag'] >>> t in s: # iterating on strings , characters ... c in t: ... print(t,c) ... gatc g gatc gatc t gatc c ctag c ctag t ctag ctag g
what if skip decode:
>>> s=[b'gatc',b'ctag'] >>> t in s: ... c in t: ... print(t,c) ... b'gatc' 71 # displays byte strings , 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
python python-2.7 dictionary python-3.4
Comments
Post a Comment