将gzipped数据作为字符串放入脚本 [英] Putting gzipped data into a script as a string

查看:177
本文介绍了将gzipped数据作为字符串放入脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绊了一个 Lorem Ipsupm 生成器,我承认,这很酷。

I snagged a Lorem Ipsupm generator last week, and I admit, it's pretty cool.

我的问题:有人可以告诉我一个关于如何上述脚本的作者的教程能否将gzipped文件的内容作为字符串发布到他们的代码中?我一直在获取压缩常规文件的示例,我在这里感到失望。

My question: can someone show me a tutorial on how the author of the above script was able to post the contents of a gzipped file into their code as a string? I keep getting examples of gzipping a regular file, and I'm feeling kind of lost here.

对于什么值得,我有另一个模块是非常相似的(它生成随机名称,公司等),现在它从一对夫妇不同的文本文件读取。我更喜欢这种方法;它在我的项目中需要少一个子目录来放置数据,它也为我提供了一种新的方式。

For what it's worth, I have another module that is quite similar (it generates random names, companies, etc), and right now it reads from a couple different text files. I like this approach better; it requires one less sub-directory in my project to place data into, and it also presents a new way of doing things for me.

我是新的流,IO类型等。随意把我的膝盖上的链接。

I'm quite new to streams, IO types, and the like. Feel free to dump the links on my lap. Snipptes are always appreciated too.

推荐答案

我不确定你要的是什么,但这里有一个刺。

I'm not sure exactly what you're asking, but here's a stab...

lipsum.py 将压缩数据作为Base64编码文本的内容包含在其代码中。 Base64 是一种使用可打印的ASCII字符表示二进制数据的编码机制。它可以用于在Python代码中包含二进制数据。它更常用于在电子邮件附件中包含二进制数据...下次有人向您发送图片或PDF文档时,查看原始消息,您将看到非常相同的东西。

The author of lipsum.py has included the compressed data inline in their code as chunks of Base64 encoded text. Base64 is an encoding mechanism for representing binary data using printable ASCII characters. It can be used for including binary data in your Python code. It is more commonly used to include binary data in email attachments...the next time someone sends you a picture or PDF document, take a look at the raw message and you'll see very much the same thing.

Python的 base64 模块提供了用于在base64和数据的二进制表示之间转换的例程...和一旦你有数据的二进制表示,无论是从文件中读取还是解码嵌入代码中的字符串,都没有什么重要。

Python's base64 module provides routines for converting between base64 and binary representations of data...and once you have the binary representation of the data, it doesn't really matter how you got, whether it was by reading it from a file or decoding a string embedded in your code.

Python的 gzip 模块可用于解压缩数据。它期望一个类似于文件的对象...并且Python提供了 StringIO 模块来在正确的方法集中包装字符串,使它们像文件一样。您可以在 lipsum中查看。 py 在以下代码中:

Python's gzip module can be used to decompress data. It expects a file-like object...and Python provides the StringIO module to wrap strings in the right set of methods to make them act like files. You can see that in lipsum.py in the following code:

sample_text_file = gzip.GzipFile(mode='rb',
    fileobj=StringIO(base64.b64decode(DEFAULT_SAMPLE_COMPRESSED)))

这是创建一个 StringIO 包含存储在 DEFAULT_SAMPLE_COMPRESSED 中的base64编码值的二进制表示形式的对象。

This is creating a StringIO object containing the binary representation of the base64 encoded value stored in DEFAULT_SAMPLE_COMPRESSED.

此处提及的所有模块都在 Python标准库的文档中介绍。

All the modules mentioned here are described in the documentation for the Python standard library.

我不建议在你的代码中内嵌类似这样的数据作为一个好主意,除非你的数据是小的,相对静态。

I wouldn't recommend including data in your code inline like this as a good idea in general, unless your data is small and relatively static. Otherwise, package it up into your Python package which makes it easier to edit and track changes.

我回答了正确的问题吗?

Have I answered the right question?

这篇关于将gzipped数据作为字符串放入脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆