I started writing io_bit.py and io_midi.py in Python 2, so I took care of them for 3.
Related) http://d.hatena.ne.jp/yoya/20141106/io_midi
--Specify "b" to open the file as binary. (It works even if it is not 2)
X data = open(file).read() O data = open(file,"rb").read()
――By the way, this is the error that appears in 3.
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", line 313, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 13: invalid continuation byte
--Since the cut out data is also binary (byte string), add b like data == b "~" in the comparison check.
X chunk == "MThd" O chunk == b"MThd"
--has_key cannot be used, so use in.
X a.has_key("x") O "x" in a
--Rewrite cutting out 1 byte from a byte string to specify a range.
X bytes [i] # ← In this case, the behavior is different between 2 and 3. O bytes[i:i+1]
$ python2 -c 'print(b"AB"[1])' B $ python3 -c 'print(b"AB"[1])' 66 $ python3 -c 'print(b"AB"[1:2])' b'B'
--Fixed hard tab to soft tab. (2 works on hard tabs)
Recommended Posts