・2023/01/09
【互換性無】Python 2用のプログラムを Python 3に移行する方法
(No module named 'StringIO'、NameError: name 'unicode' is not defined等の解決方法)
Tags: [Raspberry Pi], [電子工作]
● Python 2用のプログラムを Python 3に移行する方法
Python 3は Python 2と互換性が無い。
Python 2用のプログラムは Python 3でエラーが出て動かない場合が多い。
$ python -V
Python 3.9.2
● No module named 'StringIO'
from StringIO import StringIO
↓
from io import StringIO
or
from StringIO import StringIO
↓
from io import BytesIO
● TypeError: string argument expected, got 'bytes'
pycurl.error: (23, 'Failure writing output to destination')
from StringIO import StringIO
↓
from io import BytesIO
buffers = StringIO()
↓
buffers = BytesIO()
● TypeError: cannot use a string pattern on a bytes-like object
body = buffers.getvalue()
↓
body = buffers.getvalue().decode('utf-8')
● NameError: name 'unicode' is not defined
if isinstance(url, unicode):
↓
if isinstance(url, bytes):
Tags: [Raspberry Pi], [電子工作]
[HOME]
|
[BACK]
リンクフリー(連絡不要、ただしトップページ以外は Web構成の変更で移動する場合があります)
Copyright (c)
2023 FREE WING,Y.Sakamoto
Powered by 猫屋敷工房 & HTML Generator
http://www.neko.ne.jp/~freewing/software/migrate_python_2_to_3/