前提・実現したいこと
bytes型のある変数をstr型に変換しtextファイルに書き込み、ファイルから元のbytes型の変数を取得を目指しています。
元のbytes型の変数内にエスケープである''が含まれているのでstrに変換しもう一度bytes型に変換すると''の部分がうまく処理できずに元の変数に戻りません。
どのようにすれば元のbytes型変数に戻すことができるでしょうか。
該当のソースコード
python
1bytes_data = b'\x0c\x8e\x98\x92\x90\x82\xe4s' 2str_data = str(bytes_data) 3bytes_data_from_str1 = bytes(str_data, encoding='utf-8') 4bytes_data_from_str2 = bytes(str_data.rstrip().strip("b'"), encoding='utf-8') 5 6print('bytes_data\n', bytes_data) 7print('str_data\n', str_data) 8print('bytes_data_from_str1\n', bytes_data_from_str1) 9print('bytes_data_from_str2\n', bytes_data_from_str2)
bytes_data b'\x0c\x8e\x98\x92\x90\x82\xe4s' str_data b'\x0c\x8e\x98\x92\x90\x82\xe4s' bytes_data_from_str1 b"b'\x0c\x8e\x98\x92\x90\x82\xe4s'" bytes_data_from_str2 b'\x0c\x8e\x98\x92\x90\x82\xe4s'
補足情報
Python 3.6.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/21 05:52