简体   繁体   English

在 Python 中解码一列 Base64 字符串

[英]Decode a column of Base64 strings in Python

Situation: I have a column that I'll be extracting from SQL with hundreds of thousands of Base 64 strings that I would like to decode.情况:我有一个列,我将从 SQL 中提取包含数十万个我想解码的 Base 64 字符串。

One decoded string looks like this:一个解码后的字符串如下所示:

b'{"a":1,"b":2,"c":3,...}'

Objective: How can I simply extract the value of the first dictionary.目标:如何简单地提取第一个字典的值。 In this case, this would be 1. I would like the results to end up in a numpy array or df.在这种情况下,这将是 1。我希望结果以numpy数组或 df 结束。

I found the following code to help me decode one string but unsure how to use it for every row in a column.我发现以下代码可以帮助我解码一个字符串,但不确定如何对列中的每一行使用它。

import base64
coded_string = '''value to decode'''
base64.b64decode(coded_string)

Assuming your snippet is well-formed Python (although, this seems more likely to be JSON), you can use literal_eval :假设您的代码段是格式良好的 Python(尽管这似乎更可能是 JSON),您可以使用literal_eval

from ast import literal_eval
from base64 import b64decode
from pprint import pprint

binary = b64decode(some_str)

lib = literal_eval(binary.decode('utf8'))
pprint(lib)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM