2014-02-17 30 views
-1

我想从Sqlite进行查询并包含\ n,我期望python将其转换为换行符,但它不会。而且我也在数据库中将\ n更改为\ n,但仍然无法转换。为什么Python在从Sqlite查询时不会将 n转换为换行符?

cursor.execute('''SELECT test FROM table_name ''') 
for row in cursor: 
     self.ui.textEdit.append(row[0]) 
     # or 
     print row[0] 

我也试过unicode(row[0])并没有工作。我很惊讶,在网络上没有一个简单的解决方案。

+0

如果它是一个文字'\ n',它不会。 –

+0

@jayanth ok,但即使它不适用于\\ n – jesy2013

回答

0

SQLite和Python都没有转换字符串中的字符(\在源代码中写入的Python字符串中的转义除外)。 如果你正确处理换行符,换行符正确工作:

>>> import sqlite3 
>>> db = sqlite3.connect(':memory:') 
>>> c = db.cursor() 
>>> c.execute('create table t(x)') 
>>> c.execute("insert into t values ('x\ny')") 
>>> c.execute("insert into t values ('x\\ny')") 
>>> c.execute("select * from t") 
>>> for row in c: 
... print row[0] 
... 
x 
y 
x\ny 
相关问题