Python含め、プログラミング初心者です。
Pythonで画像のようなcsvファイルを読み込み、バイナリファイルに書き込みたいです。
以下のようなプログラムを実行すると、エラーになりました。
data_r = 'sample2.csv'
data_w = 'sample1.aesx'
with open(data_r) as f:
l_strip = [s.strip() for s in f.readlines()]
print(l_strip)
with open(data_w, mode = 'wb') as h:
h.write(l_strip)
with open(data_w) as h:
print(h.read())
エラーメッセージ ['123,1,0,0,117,98,120,45,97,108,112,104,97,2,0,0,0,3,0,0,0,0,0,8,0,0,8,0,0'] Traceback (most recent call last): File "read.py", line 8, in <module> h.write(l_strip) TypeError: a bytes-like object is required, not 'list'
試したこと
f = open("sample2.csv", "rb")
data = f.read()
f.close()
print(data)
でターミナルに出力し、出力結果の必要な部分をコピペして以下のプログラムに代入すると実行することができました。
結果は画像の通りです。
data = [123,1,0,0,117,98,120,45,97,108,112,104,97,2,0,0,0,3,0,0,0,0,0,8,0,0,8,0,0]
with open('sample1.aesx', 'wb') as f:
for d in data:
f.write(d.to_bytes(1, byteorder='big'))
この「csvファイルの読み込み→バイナリファイルへ書き出し」を、コピペなしで一つのプログラムで完結させたいです。
皆様のご助力、お願い致します。