首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用python制作锅炉板文本

用python制作锅炉板文本
EN

Stack Overflow用户
提问于 2015-03-17 14:44:16
回答 3查看 124关注 0票数 0

在我的应用程序中,我希望能够在多行textctrl中显示预定义的段落。

我假设文本将保存在文本文件中,并且我希望通过提供一个密钥来访问它;例如,键101下保存的文本可能是几个与狮子有关的段落,而在键482下可能有几个段落涉及海狮的食物。

有人能建议一种适当的方式来保存和检索这种形式的文本吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-17 15:43:31

看一下使用sqlite3,我相信它对于您所需的东西是完美的,而不需要使用完整的数据库路线。下面是创建数据库的命令行中的一个示例,该命令行用键和一些文本填充数据库,然后打印出内容。在python中为sqlite3编写代码很简单,而且有很好的文档记录。

代码语言:javascript
复制
$ sqlite3 text.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE "txt" ("txt_id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE  DEFAULT 1, "the_text" TEXT);

sqlite> insert into txt ("txt_id","the_text") values (1,"a great chunk of text");

sqlite> insert into txt ("txt_id","the_text") values (2,"Elephants are large mammals of the family Elephantidae and the order Proboscidea. Two species are traditionally recognised, the African elephant (Loxodonta africana) and the Asian elephant (Elephas maximus), although some evidence suggests that African bush elephants and African forest elephants are separate species (L. africana and L. cyclotis respectively). Elephants are scattered throughout sub-Saharan Africa, South Asia, and Southeast Asia. Elephantidae are the only surviving family of the order Proboscidea; other, now extinct, families of the order include mammoths and mastodons. Male African elephants are the largest surviving terrestrial animals and can reach a height of 4 m (13 ft) and weigh 7,000 kg (15,000 lb). All elephants have several distinctive features the most notable of which is a long trunk or proboscis, used for many purposes, particularly breathing, lifting water and grasping objects. Their incisors grow into tusks, which can serve as weapons and as tools for moving objects and digging. Elephants' large ear flaps help to control their body temperature. Their pillar-like legs can carry their great weight. African elephants have larger ears and concave backs while Asian elephants have smaller ears and convex or level backs.");

sqlite> select * from txt;

1|a great chunk of text

2|Elephants are large mammals of the family Elephantidae and the order Proboscidea. Two species are traditionally recognised, the African elephant (Loxodonta africana) and the Asian elephant (Elephas maximus), although some evidence suggests that African bush elephants and African forest elephants are separate species (L. africana and L. cyclotis respectively). Elephants are scattered throughout sub-Saharan Africa, South Asia, and Southeast Asia. Elephantidae are the only surviving family of the order Proboscidea; other, now extinct, families of the order include mammoths and mastodons. Male African elephants are the largest surviving terrestrial animals and can reach a height of 4 m (13 ft) and weigh 7,000 kg (15,000 lb). All elephants have several distinctive features the most notable of which is a long trunk or proboscis, used for many purposes, particularly breathing, lifting water and grasping objects. Their incisors grow into tusks, which can serve as weapons and as tools for moving objects and digging. Elephants' large ear flaps help to control their body temperature. Their pillar-like legs can carry their great weight. African elephants have larger ears and concave backs while Asian elephants have smaller ears and convex or level backs.

sqlite> 
票数 0
EN

Stack Overflow用户

发布于 2015-03-17 14:47:56

将文本保存在文本文件中,使用open()打开文本,然后使用read()读取结果。

例如:

代码语言:javascript
复制
TEXT_DIRECTORY = "/path/to/text"
def text_for_key(key):
    with open(os.path.join(TEXT_DIRECTORY), str(key)) as f:
        return f.read()

或者,如果您想要一个大文件而不是许多较小的文件,只需将其存储为JSON或其他易于读取的格式:

代码语言:javascript
复制
import json
with open("/path/to/my/text.json") as f:
   contents = json.load(f)

def text_for_key(key):
    return contents[key]

那么您的文件将看起来像:

代码语言:javascript
复制
{101: "Lions...",
 482: "Sea lions...",
...}
票数 0
EN

Stack Overflow用户

发布于 2015-03-17 15:21:13

我建议使用泡菜。它比json更快,保存文件的方法也更酷。

警告泡菜模块并不是为了防止错误或恶意构造的数据。永远不要对从不可信或未经身份验证的源接收的数据进行解密。

代码语言:javascript
复制
import pickle
import os

class Database(object):

    def __init__(self, name='default'):
        self.name = '{}.data'.format(name)
        self.data = None
        self.read()

    def save(self):
        with open(self.name, 'wb') as f:
            self.data = pickle.dump(self.data, f)

    def read(self):
        if os.path.isfile(self.name):
            with open(self.name, 'rb') as f:
                self.data = pickle.load(f)
        else:
            self.data = {}

    def set(self, key, value):
        self.data[key] = value

    def get(self, key):
        return self.data[key]

使用:

代码语言:javascript
复制
database = Database('db1')
database.set(482, 'Sea lions...')
print(database.get(482))
database.save()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29102127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档