0

I have done some operations with a file using python. Now all i have to is to create one table with two columns...one is for msgid and another is for msgstr...all msgids should be stored in msgid column and all msgstrs should be stored in msgstr column..

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
14
  • I think this is probably too broad of a question for stackoverflow. Please review a python sqlite tutorial (for example zetcode.com/db/sqlitepythontutorial) and ask any specific questions you may have. Commented Mar 19, 2015 at 12:31
  • You want to store the whole file in the DB or save a link/path to it? Commented Mar 19, 2015 at 12:32
  • Your .po file has no inherent structure, and it also includes blank lines and meta data. I could guess that you want to store msgid and msgstr as columns in a table of the database, but you need to make clearer your requirements. Commented Mar 19, 2015 at 12:36
  • I want to store the whole file in the DB Commented Mar 19, 2015 at 12:36
  • @mhawke.... you got my point.... plz help me Commented Mar 19, 2015 at 12:38

1 Answer 1

1

There are 2 parts to this:

  1. Extracting the msgid and corresponding msgstr values from the .po file.
  2. Inserting the msgid and msgstr into a table in the SQLite database.

For part 1, I suggest using the babel module. You can install it with

pip install babel

Use the babel.messages.pofile.read_po() function to read the .po file. 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:

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 msgids with empty msgstrs. 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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.