Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

[V8] Error when decode a file stored on a binary field

By
David Diz
on 6/22/16, 7:03 AM 309 views

 

I have the transient object:

class new_import(models.TransientModel):
     _name = 'new.import'
     data_file = fields.Binary('File')


I call a wizard to load the file in the binary field and then i want to parse it.

In the function to parse the file i do:

data_file = base64.b64decode(self.data_file)
lines = data_file.split('\n')
for line in lines:
    result = line[6:11]

but when i read the lines if there is a special character the length of the line isn't the same in all lines,

the file is utf8 in the origin

this must be an error when save the file in binary and then decode back to string

If i read the file directly with python with out store it in a binary field i can read it correctly:

codecs.open(file, mode='r', encoding='utf-8')
result = line[6:11].encode('utf-8')

How can i avoid storing the file in base64 or how can i after decode correctly to string?

Thanks in advance

0
David Diz
On 6/26/16, 12:38 PM

Aswering Juan Vicente Pascual, that's not a solution.

The original txt file has 412 carachters per line. When i stored it in a binary field and then transform back in a string (

base64.b64decode(self.data_file)), lines has 411,412,413,414 or 415 in function of the special characters they have.

Some information is missed when the file is stored in a binary field.

Other option is that the user select the file in a form without using a binary field.

how can i do that?

then i could parse the file correctly: 

result = codecs.open(os.path.abspath(txt_file), mode='r', encoding='utf-8')

0
Juan Vicente Pascual
On 6/22/16, 9:11 AM

Try,

result = base64.encodestring(line[6:11].encode("iso-8859-1"))


Kind regards.


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/22/16, 7:03 AM
Seen: 309 times
Last updated: 6/26/16, 12:38 PM