我正在编写一个从设备接收base 64字符串的程序。该字符串是一个编码的json文件,我想解释它。
如果我这样做(使用python base64 lib):
decoded = base64.decodestring(base64str)
print(decoded)
然后它打印出一堆乱码,因为原始数据不是字符串,而是文件。
我可以看到乱码之间文件的名称,所以我想我正确地收到了数据,但需要将其直接解释为文件而不是字符串。
我已经四处寻找并且没有设法让它发挥作用。试图将字符串保存到文件显然只是将所有乱码保存到文件
非常感谢您对如何做到这一点的帮助!
基础64字符串:
UEsDBBQACAAIAAAAIQAAAAAAAAAAAEEfAAALACAAbzNkM3h4Lmpzb25VVA0ABwAAAAAAAAAAAAAAAHV4CwABBAAAAAAEAAAAAO1ZbW/iOBD+vr8C5ev1UBIKtNwnlpc2UnlZoHvb3a6QSQxYm8RZxymwK/77zTiBJkB4OfWk+1CkqrbzPDOT8Xg8dn5/KMBPqweBy2wiGfdDrVb4pkbx93vbUrgmDW3BAsQBTNOuso8tj8yoaHB/ymaRIAnsWwa0L3VLr0eSt5YBDyNBO2T5wP3ZcM6FbBNbcgGSyvrVGUzm7zPNM4gDOqWC+jbtc+bLL8C6ubmY9gS0SuUS2qBnoS9/P2vQCp+12jdoMgca+tWzNhM8CpK2XAUUmgCktnzWYGTBHDmHIaOEz+eUzeYSuzp2iT9zaUK1qS+pGC+hCy+17a+gX6msv6+1SywegR1gct5kNObE96l7DOGyIGD+7COXknsANErGCWgjmnDmJI76AnMMlhf+LBWvdfPGLNE/SjdXBXxA8BULu+NPeYSnHMLXPMLXA4Rc722Mf6BTeYY7Bjh96I1q+QRyxINj8rgvmR/xKExPHxCmxA1pDqnlk4lL6x4kAhk5tMGFgCiLV7AU0XFam4SyLejPCMJjdZaiNnMhALfqVOY4S5PiNVkoCUTi2TRcMWyapLg9rafNzfB3tR+lJ94fMY+qDKbnzVoa+QAaMNYNs/wXUvICLM0ZoG1Aus5T0BZEoZQdORiI7rrr8gV1HlrNNMEsFfNyKCwV5kXe1qtoQx52QEPuRklY5Rk6DOBViBtPNeAObxfrc9gnUtV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qfCpbDPqOh+VZOPI45YPKxCOzCEC9TQQl5p6tTbzWTinTof599x1kpskIwNe4rILmd2A5DcZcPXJ5Sjg6RTg61HASBA/PK5EQY6rUZBdRVavSSdQqdnx1VT2PhqeggjwYGrqhwsm7fmeFHU/lUxSegasft1xBDg8/ri2y8PUiS6GfcALtt9pdlCb43+XLgoh9UMu0iHQC/D+DcxP8lia2e/i+2H8bGSkmX0Shgsoe1V4Q57EK4OdK/Et5p6E8z0+HgoEl9zm7mcqwjh4SzsI2Pv7XOCteFnXM3E0JC8UygLJhVoXYU/5eevgnbkYUoEvMqCB+j7IXAoRPZ2qBWeU84Bofz4wRJvR/1CGoOlp63D3QM9GgrYFhzoKAWa1UqweAZknQFbnSwU9YRbL5u3t7U21elvKQbpuhMjrYvUWf2apqqc+T2qPQbI09aJplPRy8qsoRLJHaW0mvAURceYoVgBpXG8SXZdKmNgf2UzXqTeSeEWKrtd0s6YbtWu9Vq7WStfp2U/4w4CqwMlMLMwms63+y3VKmHFrFo3KTVEvVm61g+A7iMEFWWXBpm4cRg+jCZjQISG+gmaWy8XNX/qrC94/N+8bfS197bz+sP7wD1BLBwiTXw4cIAcAAEEfAABQSwECFAMUAAgACAAAACEAk18OHCAHAABBHwAACwANAAAAAAAAAAAApIEAAAAAbzNkM3h4Lmpzb25VVAUABwAAAAB1eAAAUEsFBgAAAAABAAEARgAAAHkHAAAAAA==
"解码"字符串:
!A ux o3d3xx.jsonUT �Ymo�8������P ��'��6RyY�{�ݮ�I m��LӮ��-�̨hp�f� X�[�/uK�G���#A;d����p΅l[r����L��3�3�:���6�s��/�������J�ڠg�/?k� ���7h2�ճ6< ��\���|�`d�9�!�������:v�?siB��/�/�
/��_�����K,�`r�d4����{ J� � `��#��{4J� h#�p�$��s ��ԇY��\����o�J�:��6�<wH�xR��{,���� ��,�u��,�?J7W|@� i�哉K�$9����(�W��qZ���-��cu��6s!��P���Y�6G����R�v�-;́B��ĭ�Vܯ7��,��y�,�ψ@1�zH~ ��8~.��'Q��}����6��H���ZV(j7�`����漏t�(������:�S�^��o^t��fl?I��9N�,�G���A�ŷ ��a���\�����Nr���p�]ڍ��� ��R�,��m@��S�D��9����uZ�4�,�r(,�E�֫hCv@C�FIX�: ���Z�0� �z�fI /%��5� �U��o�s�'R�p�yT��h.h8�.&�x��Q/��_���
�q�_�-� -���`F���)F]� ���� ��tXU�*�`M�2�rBd2�DFx��������3�A�h4jf�aKΩ�l3�:�d���+��!�4��z�6�Y8�N����u��$#^���0:q��#LHay�F��%-�I�j^%M��THFc��uW}�;n}nuGc�S�k��O���Y���[��V3�@)5O*��/�9h՛O�vo0
⠞�ߴ�Q�a�~��|R�a�^�<�Ƈ6��+ō�ڠ''�1s�+5��+�� ��� �݀�7p���(����Q�H?<�DA��Q�]EV�I'P����T�>��`j� c 4�A��W>%�ؕ�ޭ7�,7[� &���u?�LRz�~�q8<�����ԉ.�}� �.Q�Y\H�Tѿ+�K�R �0#��fZ�fv���#W�9��ZM`�KV<���?u�M��iʅGԫ#�!�@��X�1z�|"V1� ��ivP���. !�C.�!� g#d��9$UhR�z�Z`� �O�X�����a�ld��}�"�
({UxC��+��+�-构�=> ����g*�8xK���\�xY�3q4$/�ɅZaO�y��R�/2���>�\
=��g��h>0D���P���i�p�@�F���: f�R��'@V�K=a�����M�z[�A�n���b�f���>Oj�A�4��i��r�(D�Gim&�q�(Vi\o]�J���Lש7�xE���t��k�V��J���O�À���L,�&����uJ�qk��MQ/Vn���;��Ye��nF� ��!!��f����_��
�?7������P�_ A!�_ A ��o3d3xx.jsonUTuxPKFy
希望有一种提取和保存文件的方法,而不必转换字符串。
答案 0 :(得分:4)
当我print(decoded[:20])
时,我实际上已经证明了这一点:
b'PK\x03\x04\x14\x00\x08\x00\x08\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00'
PK\x03\x04
是一个&#34;幻数&#34;对于zip file。所以这样对待它。将其保存到名为&#34; mydata.zip&#34;的文件中。然后像提取任何zip文件一样提取它,或者在Python中处理它,例如:
...
decoded = base64.decodestring(base64str)
import io, zipfile, json, pprint
with zipfile.ZipFile(io.BytesIO(decoded)) as zf:
for name in zf.namelist():
print(name)
with zf.open(name) as f:
data = json.loads(f.read().decode())
pprint.pprint(data)
使用您的数据,首先打印文件名&#34; o3d3xx.json&#34;然后是数据:
{'Applications': [{'Description': '',
'ImagerConfiguration': [{'AutoExposureMaxLongShortFactor': 50,
'AutoExposureMinLongShortFactor': 2,
(... lots and lots and lots of data ...)
'StaticIPv4SubNetMask': '255.255.255.0',
'UseDHCP': False}}