Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - generate csv file in memory and then encode its data into base64?

I need to generate csv file like data in memory and then encode it to base64, so I could save it. So basically I don't want to create file in hard disk for that. Now I solve this by creating csv file then encoding its data, saving it and then just simply removing csv file (because it is no longer needed). But is there a way to skip file creation, but save data the same way? I mean that data would be used to open csv file again using base64.

import base64
import csv
from StringIO import StringIO
import os

def test_binary(self):
    mylist = [['a', 'b'], ['c', 'd']]
    with open("test.csv", "wb") as f:
        writer = csv.writer(f)
        writer.writerows(mylist)   

    myfile = open('test.csv', 'r')
    stream = StringIO(myfile.read())

    encoded = base64.b64encode(stream.getvalue())

    self.test = encoded
    myfile.close()
    os.remove('test.csv')        
like image 900
Andrius Avatar asked Oct 16 '15 08:10

Andrius


People also ask

How do I encode to base64 in Python?

In order to encode the image, we simply use the function base64. b64encode(s) . Python describes the function as follows: Encode the bytes-like object s using Base64 and return the encoded bytes.

What is base64 b64decode in Python?

b64decode() in Python. With the help of base64. b64decode() method, we can decode the binary string into normal form.


1 Answers

You could pass StringIO() to the csv writer directly:

>>> import base64
>>> import csv
>>> from io import StringIO
>>> mylist = [['a', 'b'], ['c', 'd']]
>>> f = StringIO()
>>> csv.writer(f).writerows(mylist)
>>> base64.b64encode(f.getvalue().encode())
b'YSxiDQpjLGQNCg=='

Python 2

>>> import base64
>>> import csv
>>> from StringIO import StringIO
>>> mylist = [['a', 'b'], ['c', 'd']]
>>> f = StringIO()
>>> csv.writer(f).writerows(mylist)
>>> base64.b64encode(f.getvalue())
'YSxiDQpjLGQNCg=='
like image 130
jfs Avatar answered Sep 20 '22 08:09

jfs