在 Python 脚本中存储数据块的 Pythonic 方式是什么? [英] What's the Pythonic way to store a data block in a Python script?

查看:42
本文介绍了在 Python 脚本中存储数据块的 Pythonic 方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl 允许我在脚本中使用 __DATA__ 标记来标记数据块的开始.我可以使用 DATA 文件句柄读取数据.在脚本中存储数据块的 Pythonic 方式是什么?

Perl allows me to use the __DATA__ token in a script to mark the start of a data block. I can read the data using the DATA filehandle. What's the Pythonic way to store a data block in a script?

推荐答案

这取决于您的数据,但 dict 文字和多行字符串都是非常好的方法.

It depends on your data, but dict literals and multi-line strings are both really good ways.

state_abbr = {
    'MA': 'Massachusetts',
    'MI': 'Michigan',
    'MS': 'Mississippi',
    'MN': 'Minnesota',
    'MO': 'Missouri',
    }

gettysburg = """
Four score and seven years ago,
our fathers brought forth on this continent
a new nation, 
conceived in liberty
and dedicated to the proposition
that all men are created equal.
"""

这篇关于在 Python 脚本中存储数据块的 Pythonic 方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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