I have a list of lists that I want saved to s3.
Originally, when I saved these locally, I converted them to a csv like this:
from csv import reader, writer
words_list = [['here', 'are', 'some', 'words']['these', 'are', 'in', 'a', 'list']['i', 'love', 'lists']]
# Save list of words list
with open("data/words.csv", "w") as f:
wr = csv.writer(f)
wr.writerows(words_list)
# Retrieve list of words list
with open("data/words.csv", 'r') as read_obj:
csv_reader = reader(read_obj)
words_list = list(csv_reader)
I am able to save a csv version of the list of lists to s3, I think, using this, which just takes the csv I have saved locally already:
import boto3
session = boto3.Session(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
s3 = session.resource('s3')
bucket = 'bucket_name'
path = 'data/words.csv'
key = 'data/words.csv'
s3.meta.client.upload_file(key, bucket, path)
However, I've been unable to work out how to read the csv back and convert it to the original list of lists. For example, I've tried this:
# Retrieve from s3
import csv
from io import StringIO
bucket = 'bucket_name'
path = 'data/words.csv'
client = boto3.client('s3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
csv_obj = client.get_object(Bucket=bucket, Key=path)
body = csv_obj['Body']
csv_string = body.read().decode('utf-8')
But this gets me a string without a sense of where the separate lists start and end, a bit like: "here,are,some,words,these,are,in,a,list,i,love,lists"
I'm not wedded to this approach - I'd be happy with anything that allows me to save the list of lists in a recoverable format in s3. How can I achieve this?
Thanks!