python - 如何将base64解码从python2转换为python3
问题描述
我正在开发一个下载和处理字幕文件的脚本。文件以 gzip 格式出现,文档说:使用:gzinflate(substr(base64_decode($subs_b64_data_from_xmlrpc),10))。在 Python 2 中这很好用,我最终得到一个包含字幕文本的 str 。
compressed_data = download_data['data'][0]['data'].decode('base64')
sub_text = gzip.GzipFile(fileobj=io.BytesIO(compressed_data)).read()
print(sub_text)
给我这个str:
841
01:52:28,344 --> 01:52:29,878
Sweet dreams, angel.
842
01:53:44,844 --> 01:53:46,377
I love you, honey.
当我使用 python 3 时,我根据 python 3 文档将 .decode('base64') 更改为 base64.b64decode() 。
compressed_data = base64.b64decode(download_data['data'][0]['data'])
sub_text = gzip.GzipFile(fileobj=io.BytesIO(compressed_data)).read()
print(sub_text)
但现在我的数据最终成为一个字节对象,打印的数据如下所示:
00:33:30,009\r\ncalled Babies I\r\nDon\'t Care About.\r\n\r\n694\r\n00:33:31,305 --> 00:34:31,557\r\n
我怎样才能在 python3 中正确地做到这一点,所以我最终也会得到一个 str ?
解决方案
如果 sub_text 以字节为单位,你应该试试这个......
print(sub_text.decode('utf-8'))
推荐阅读
- asp.net - 哪种数据类型适合此类别/子类别行为?
- docker - python - 如何将 slack 令牌从 dockerfile 传递给 python 应用程序
- asp.net-core - 通过 .NET Core 中的配置文件配置日志记录
- javascript - 如何在插值中平滑地从较高值到较低值,反之亦然
- ios - 在 iOS 中的初始屏幕之前,React Native 随机内部屏幕闪烁几秒钟
- javascript - onChange 时自动提交选择
- apache - Xampp,Apache:我在运行 apache 时遇到问题。如何使PID不同PID 1908?
- python - 多仓库 docker-compose
- mysql - 如果存在则获取下一个问题,如果不存在则返回开始 - mysql
- python - Mxnet 数据类型是 float64,但一直说是 float32