021_py_mojisuuti_jisyo - kotaproj/study_zenpan GitHub Wiki
>>> moji = '123'
>>> int(moji)
123
>>> ba = b'0f'
>>> ba
b'0f'
>>> int(ba, 16)
15
>>> a = 127
>>> hex(a)
'0x7f'
>>> a = 127
>>> format(a, '08x')
'0000007f'
>>> format(a, '08X')
'0000007F'
http://ni4muraano.hatenablog.com/entry/2017/02/05/102919
num = 255
num.to_bytes(2, 'big') # 2バイト、ビッグエンディアン、b'\x00\xff'と出力される
int.from_bytes(b'\x00\xff', 'big') # 255と出力される
import ast
str = "{'名前':'太郎', '身長':'165', '体重':'60'}"
dic = ast.literal_eval(str)
print(dic['名前'])
print(type(dic))
↓実行結果↓
太郎
<class 'dict'>
>>> import ast
>>> s = "{'name':'twilite','temp':'7.3','hum':'77.16','pressure':'998'}"
>>> print(ast.literal_eval(s))
{'name': 'twilite', 'temp': '7.3', 'hum': '77.16', 'pressure': '998'}
>>>
>>> 'abcd'.encode()
b'abcd'
>>> b'abcd'.decode()
'abcd'
>>> 'abcd'.encode(encoding='utf-8')
b'abcd'
>>> b'abcd'.decode(encoding='utf-8')
'abcd'
>>> b'\xff'.decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
>>> b'\xff'.decode('utf-8', 'replace')
'�'
>>> b'\xff'.decode(encoding='utf-8', errors='replace')
'�'
>>> str(b'\xff', encoding='utf-8', errors='replace')
'�'
>>> bytes('abcd', encoding='utf-8', errors='replace')
b'abcd'
>>> str(b'abcd', encoding='utf-8', errors='replace')
'abcd'
print(int('0o10', 0))
print(int('0x10', 0))
# 2
# 8
# 16