在我的应用程序中,我希望能够在多行textctrl中显示预定义的段落。
我假设文本将保存在文本文件中,并且我希望通过提供一个密钥来访问它;例如,键101下保存的文本可能是几个与狮子有关的段落,而在键482下可能有几个段落涉及海狮的食物。
有人能建议一种适当的方式来保存和检索这种形式的文本吗?
发布于 2015-03-17 15:43:31
看一下使用sqlite3,我相信它对于您所需的东西是完美的,而不需要使用完整的数据库路线。下面是创建数据库的命令行中的一个示例,该命令行用键和一些文本填充数据库,然后打印出内容。在python中为sqlite3编写代码很简单,而且有很好的文档记录。
$ 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> 发布于 2015-03-17 14:47:56
将文本保存在文本文件中,使用open()打开文本,然后使用read()读取结果。
例如:
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或其他易于读取的格式:
import json
with open("/path/to/my/text.json") as f:
contents = json.load(f)
def text_for_key(key):
return contents[key]那么您的文件将看起来像:
{101: "Lions...",
482: "Sea lions...",
...}发布于 2015-03-17 15:21:13
我建议使用泡菜。它比json更快,保存文件的方法也更酷。
警告泡菜模块并不是为了防止错误或恶意构造的数据。永远不要对从不可信或未经身份验证的源接收的数据进行解密。
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]使用:
database = Database('db1')
database.set(482, 'Sea lions...')
print(database.get(482))
database.save()https://stackoverflow.com/questions/29102127
复制相似问题