I could sort out the problem. Thanks for the helps.
Here it is:
# -*- coding: iso-8859-1 -*-
import sys
import codecs
import sqlite3
f = codecs.open("suess_sweet.txt", "r", "utf-8") # suess_sweet.txt file contains two
text_in_unicode = f.read() # comma-separated words: süß, sweet
f.close()
stdout_encoding = sys.stdout.encoding or sys.getfilesystemencoding()
con = sqlite3.connect('dict1.db')
cur = con.cursor()
cur.execute('''create table IF NOT EXISTS table1 (id INTEGER PRIMARY KEY,German,English)''')
[ger,eng] = text_in_unicode.split(',')
cur.execute('''insert into table1 (id,German,English) VALUES (NULL,?,?)''',(ger,eng))
con.commit()
sentence = "The German word is: %s" %(ger,)
print sentence.encode(stdout_encoding)
con.close()
I got some help from this page (it's in German)
and the output is:
The German word is: ?süß
Still a small problem is the '?'. I thought that the unicode u'
is replaced by ?
after encoding. sentence
gives:
>>> sentence
u'The German word is: \ufeffs\xfc\xdf '
and encoded sentence gives:
>>> sentence.encode(stdout_encoding)
'The German word is: ?s\xfc\xdf '
so it was not what I thought.
A simple solution comes to my mind, to get rid of the question mark is to use the replace function:
sentence = "The German word is: %s" %(ger,)
to_print = sentence.encode(stdout_encoding)
to_print = to_print.replace('?','')
>>> print(to_print)
The German word is: süß
Thank you SO :)