Decodificar base64 en Odoo 12

Peter Otten __peter__ at web.de
Wed Mar 6 03:06:40 EST 2019


angiielovee177 at gmail.com wrote:

> El martes, 5 de marzo de 2019, 3:05:07 (UTC-6), Peter Otten escribió:
>> angiielovee177 at gmail.com wrote:
>> 
>> > El lunes, 4 de marzo de 2019, 11:07:40 (UTC-6), Peter Otten escribió:
>> >> Angie GL wrote:
>> >> 
>> >> > Hola a todos, tengo un problema al decodificar el contenido de una
>> >> > variable base64.
>> >> > 
>> >> > De esta manera lo hago:
>> >> > 
>> >> > cfdi = base64.b64decode(inv.l10n_mx_edi_cfdi)
>> >> > 
>> >> > 
>> >> > 
>> >> > Al momento de decodificar el resultado que me envía es esto:
>> >> > 
>> >> > b'\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n'
>> >> > 
>> >> > Alguien que me pueda decir que estoy haciendo mal, lo cheque en la
>> >> > consola Python3 y todo va bien, pero dentro de Odoo no lo
>> >> > decodifica.
>> >> 
>> >> What result did you expect?
>> >> 
>> >> What is the value of inv.l10n_mx_edi_cfdi? For
>> >> 
>> >> b'CgogCiAKIAogCiAKIAogCiAKIAogCiAKIAogCiAKIAogCgo='
>> >> 
>> >> you see the correct result:
>> >> 
>> >> >>> 
base64.b64decode(b'CgogCiAKIAogCiAKIAogCiAKIAogCiAKIAogCiAKIAogCgo=')
>> >> b'\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n'
>> > 
>> > El valor de la variable inv.l10n_mx_edi_cfdi es una factura en formato
>> > base64, el resultado que espero es que decodifique la factura para
>> > posteriormente mostrarlo. PERO NO LO DECODIFICA,ya verifique el valor
>> > de la variable pero no lo hace.
>> 
>> If you replace the line
>> 
>> >> > cfdi = base64.b64decode(inv.l10n_mx_edi_cfdi)
>> 
>> in your script with
>> 
>> cfdi = inv.l10n_mx_edi_cfdi
>> print(type(cfdi))
>> print(repr(cfdi))
>> cfdi = base64.b64decode(cdfi)
>> print(repr(cfdi))
>> 
>> what gets printed? Use cut and paste to post the result. If there is a
>> traceback post that, too. Thank you.
> 
> Este es el resultado de cada linea:
> 
>> cfdi = inv.l10n_mx_edi_cfdi
>> print(type(cfdi)) No imprime nada

That's strange. What does

print(isinstance(cdfi, bytes))
print(isinstance(cdfi, bytesarray))

print? If both print False, does 

print(memoryview(cdfi).tobyes())

produce the same long string that you posted below? If it's much shorter 
then the "magic" happens here. Otherwise (more likely) read on below.

>> print(repr(cfdi)) Imprime la cadena en base64, es bastabte
>> larga.(b'PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZG...

>> cfdi = base64.b64decode(cdfi) print(repr(cfdi)) Imprime b'\n\n \n \n \n
>> \n \n \n \n \n \n \n \n \n \n \n \n \n\n'
> 
> Intente con otra cadena en base64 y lo hace bien, al momento de que sea
> una factura no lo hace. ¿Qué podrá ser?, llevo días tratando de resolver
> este problema y solo no veo solución.

The output is an XML document, and if you extract only the text with the 
following little script

$ cat demo.py
import base64
from lxml import etree
data = b'PD...' # what you posted
tree = etree.fromstring(base64.b64decode(data))
print(repr("".join(tree.xpath("//text()"))))

you get something similar to what you see:

$ python3 demo.py
'\n  \n  \n  \n    \n      \n        \n          \n        \n      \n    \n  
\n  \n    \n      \n    \n  \n'

I don't know Odoo, perhaps it tries to be smart and shows XML in a "user-
friendly" way, like it would appear in a browser?

In this case I suggest that you present your problem to the Odoo support or 
an Odoo specific forum. Perhaps there is a way to switch off this feature 
and show the raw data instead.




More information about the Python-list mailing list