1. ByteIO가 csv에 string으로 저장될 때
원래 데이터에서 bytes 타입으로 잘 읽어졌었지만
csv로 저장한 후 다시 읽어보니
TypeError: a bytes-like object is required, not 'str'
TypeError: a bytes-like object is required, not 'str'
BytesIO가 읽어야 하는 객체가 string이라는 에러가 났다
2. base64.b64decode 를 둘러싼다
base64 모듈에 있는 b64decode 함수를 활용해서 BytesIO가 읽을 수 있는 객체로 바꾼다
위에 있는 내용으로는 이미지가 해독이 안된다
import ast 의 모듈에서 ast.literal_eval로 원래 string을 둘러싼 후 BytesIO로 읽는다
import pandas as pd
from io import BytesIO
import base64
from PIL import Image
import ast
new_dataframe = pd.read_csv('new_dataframe.csv')
new_dataframe.iloc[0]
image_data = new_dataframe.iloc[0]['jpg_0']
image_data = BytesIO(ast.literal_eval(image_data))
image_data
이렇게 하면 BytesIO가 인식 가능한 객체가 된다
이미지로 읽고 싶으면 Image.open을 하면 된다
Reference
https://stackoverflow.com/questions/47741235/how-to-read-bytes-object-from-csv
How to read bytes object from csv?
I have used tweepy to store the text of tweets in a csv file using Python csv.writer(), but I had to encode the text in utf-8 before storing, otherwise tweepy throws a weird error. Now, the text d...
stackoverflow.com