This question has been flagged
1 Reply
11601 Views

Hi everyone. Can anyone help me, pls!

I have my problem with base64 but i cant resolve it. What i'm doing is that i am trying to persist a file (either .DAT or .csv) on my database but upon running:

1. On my method file_writer, it says:

'file_data' : base64.encodestring(virtual_file),

File "/usr/lib/python2.7/base64.py", line 313, in encodestring for i in range(0, len(s), MAXBINSIZE):

AttributeError: StringIO instance has no attribute '__len__'

 

2. On my method csv_writer, it says:

    'name' : 'Studentlist',
  File "/usr/lib/python2.7/base64.py", line 313, in encodestring
    for i in range(0, len(s), MAXBINSIZE):
TypeError: object of type '_csv.writer' has no len()

 

Here are my methods:

1.

def file_writer(self, cr, uid, ids, context=None):
        
        virtual_file = StringIO()
        student_object = self.pool.get('student.student')
        active_id = student_object.search(cr,uid,[])
        for obj_list in student_object.browse(cr,uid,active_id,context=context):
            virtual_file.write(str(obj_list.last_name)+","+str(obj_list.first_name)+","+str(obj_list.middle_name))
        self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : base64.encodestring(virtual_file),
                            })
        return True

 

( Here, without base64 it persist into database but upon opening of downloaded file it shows something like this :

JÚâžJÚ➊{-jwjÝ1í÷5÷Î;kO|  )
    

2.

def csv_writer(self, cr, uid, ids, context=None):
        
        virtual_file = csv.writer(open("/tmp/Student.csv","wb"))
       student_object = self.pool.get('student.student')
        active_id = student_object.search(cr,uid,[])
        for obj_list in student_object.browse(cr,uid,active_id,context=context):
            virtual_file.writerow([str(obj_list.last_name),str(obj_list.first_name),str(obj_list.middle_name)])
        self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : base64.encodestring(virtual_file),
                            })
        return True

(Here, w/o base64 it shows:

  File "/usr/lib/python2.7/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

)

 

Any help is much appreciated.

 

Error image:

 

Avatar
Discard
Best Answer

Hi,

You just need to close your file which is attached with your writer object. And then you have to again open for read and pass it to the base64.encodestring() function. Please have a look on the below.

virtual_file.seek(0)

self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : base64.encodestring(virtual_file.read()),
                            })

vrtual_file.close()

Note : You have passed writer object to the base64.encodestring() function thats why you face an error.

Please try according my solution as like below.

file_point = open('/tmp/student.csv','wb')

student_object = self.pool.get('student.student')
active_id = student_object.search(cr,uid,[])
for obj_list in student_object.browse(cr,uid,active_id,context=context):
    file_point.write(str(obj_list.last_name)+","+str(obj_list.first_name)+","+str(obj_list.middle_name))

file_point.close()
file_point = open('/tmp/student.csv','rb')
file_data = 
base64.encodestring(file_point.read())
self.create(cr,uid,{
                            'name' : 'Studentlist',
                            'file_data' : file_data,
                            })

Avatar
Discard
Author

Thank you sir for the response, i tried your suggestion but the terminal says: AttributeError: '_csv.writer' object has no attribute 'close'

Is it ok for StringIO ? For the csv.writer just try without close.

Author

I have tried in my other method (file_writer method) and it rendered the view, however no object on fields file_data. In my csv_writer method it produce an AttributeError.

which AttributeErro ? can you put entire trace into your question ? It is easy to resolve your issue.

Author

This is what it produce sir: Traceback (most recent call last): File "/opt/openerp/server-7/openerp/netsvc.py", line 296, in dispatch_rpc result = ExportService.getService(service_name).dispatch(method, params) File "/opt/openerp/server-7/openerp/service/web_services.py", line 626, in dispatch res = fn(db, uid, *params) File "/opt/openerp/server-7/openerp/osv/osv.py", line 190, in execute_kw return self.execute(db, uid, obj, method, *args, **kw or {}) File "/opt/openerp/server-7/openerp/osv/osv.py", line 132, in wrapper return f(self, dbname, *args, **kwargs) File "/opt/openerp/server-7/openerp/osv/osv.py", line 199, in execute res = self.execute_cr(cr, uid, obj, method, *args, **kw) File "/opt/openerp/server-7/openerp/osv/osv.py", line 187, in execute_cr return getattr(object, method)(cr, uid, *args, **kw) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist.py", line 131, in load_alphalist return self.pool.get('alphalist.minimum.prev.employer').load_all_employees(cr,uid,ids,data,context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 155, in load_all_employees id2 = self.call_file_writer(cr, uid, ids, context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 190, in call_file_writer return self.pool.get('file.holder').csv_writer(cr,uid,ids,context=context) File "/opt/openerp/biz1_server70_addons/biz1_bir_report/biz1_alphalist_forms.py", line 1351, in csv_writer virtual_file.close() AttributeError: '_csv.writer' object has no attribute 'close'

Author

I use virtual_file.close() on my csv_writer method but it produce an error like this one: virtual_file.close() AttributeError: '_csv.writer' object has no attribute 'close'

Hi, I have update my answer. Please try it and let me know the result.

Author

Sir it produce another error, here is what it says: virtual_file.seek(0) AttributeError: '_csv.writer' object has no attribute 'seek'

May I know that which library you have import in your module ?

Author

Here are some of my imports sir: import logging import base64 import csv

Hi, I am confused because csv have all of those (close, seek, etc.) attributes but in your system it gives an attribute error. So, please try as I have given you in my updated answer.

Have you tried updated answer and then you got an error while down load "Incorrect padding" ? ?

Author

I really don't know what is going on with the code, but every time i plug csv attributes it pops up AttributeError ('_csv.writer' object has no attribute 'close' and the like.)