[英]Storing a text file into SQLite3 database using python
I have done some operations with a file using python. 我已经使用python对文件进行了一些操作。 Now all i have to is to create one table with two columns...one is for msgid and another is for msgstr...all
msgid
s should be stored in msgid
column and all msgstr
s should be stored in msgstr
column.. 现在我要做的就是创建一个包含两列的表...一个用于msgid,另一个用于msgstr ...所有
msgid
都应存储在msgid
列中,所有msgstr
都应存储在msgstr
列中。
I am very new to the programming world. 我对编程世界很陌生。 Kindly help me.
请帮助我。 I have pasted what i had done below:
我在下面粘贴了我所做的操作:
fn='ru.po'
f=open(fn)
output=[]
for line in f:
if not '#' in line:
output.append(line)
f.close()
f=open(fn,'w')
f.writelines(output)
f.close
There are 2 parts to this: 分为两个部分:
msgid
and corresponding msgstr
values from the .po file. msgid
和相应的msgstr
值。 msgid
and msgstr
into a table in the SQLite database. msgid
和msgstr
插入SQLite数据库的表中。 For part 1, I suggest using the babel
module. 对于第1部分,我建议使用
babel
模块。 You can install it with 您可以使用
pip install babel
Use the babel.messages.pofile.read_po()
function to read the .po
file. 使用
babel.messages.pofile.read_po()
函数读取.po
文件。 This will return a catalog on which you can iterate over all of the messages parsed from the file: 这将返回一个目录,您可以在该目录上遍历从该文件解析的所有消息:
from babel.messages.pofile import read_po
with open('ru.po') as po_file:
cat = read_po(po_file)
for message in cat:
if message.id:
print '{!r} -> {!r}'.format(message.id, message.string)
For part 2: 对于第2部分:
import sqlite3
conn = sqlite3.connect('catalog.db')
cursor = conn.cursor()
result = cursor.execute('CREATE TABLE ru (msgid TEXT, msgstr TEXT)')
# bulk insert the messages
messages = [(msg.id, msg.string) for msg in cat if msg.id]
result = cursor.executemany('INSERT INTO ru (msgid, msgstr) VALUES (?, ?)', messages)
assert(result.rowcount == len(messages))
conn.commit()
result = cursor.execute("SELECT * from ru where msgid = '11 inches/sec.'")
msgid, msgstr = result.fetchone()
# .encode('utf8') can be removed for Python 3
print '"{}" translates to "{}"'.format(msgid, msgstr.encode('utf8'))
msgid = 'A Samba password is required to export printer drivers'
result = cursor.execute("SELECT * from ru where msgid = ?", (msgid,))
msgid, msgstr = result.fetchone()
print '"{}" translates to "{}"'.format(msgid, msgstr.encode('utf8'))
Output 产量
"11 inches/sec." translates to "11 дюймов/с" "A Samba password is required to export printer drivers" translates to "Для экспорта драйверов принтера требуется пароль Samba"
You might notice that there are lot of msgid
s with empty msgstr
s. 您可能会注意到,有许多
msgid
带有空的msgstr
。 If you don't want them, then modify 如果您不想要它们,请修改
messages = [(msg.id, msg.string) for msg in cat if msg.id]
to 至
messages = [(msg.id, msg.string) for msg in cat if msg.id and msg.string]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.