从 Cassandra 查询结果中删除换行符
Remove newline characters from Cassandra query results
我正在使用 python Cassandra 驱动程序查询数据库。
这是我 运行 的代码:
from cassandra.cluster import Cluster
from cassandra.query import dict_factory
ips = [...]
cluster = Cluster(ips)
session = cluster.connect()
session.row_factory = dict_factory
session.set_keyspace("mykeyspace")
response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';")
在输出中,我在单词前面出现了奇怪的“n”,以前是换行符。
示例:
"Over the wintry nforest, winds howl in rage nwith no leaves to blow."
有办法解决这个问题吗?
问题出在您的数据摄取中,或者任何正在消耗 response
而不是驱动程序读取中的问题。它是存储在数据库中的 n
,而不是 \n
字符。 python 驱动程序不会对原始字节进行转义或执行任何操作,而是将其转换为字符串:https://github.com/datastax/python-driver/blob/9869c2a044e5dd76309026437bcda5d01acf99c7/cassandra/cqltypes.py#L693
如果你插入一个 \n
你会得到一个回来:
session.execute('CREATE TABLE mydb (mykey text PRIMARY KEY, mycolumn text);')
session.execute("INSERT INTO mydb (mykey, mycolumn) VALUES ('xyz', 'a\nb');")
response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';")
for line in response:
print line['mycolumn']
正确输出:
a
b
有没有什么东西带走 response
并在之后转义?
我正在使用 python Cassandra 驱动程序查询数据库。 这是我 运行 的代码:
from cassandra.cluster import Cluster
from cassandra.query import dict_factory
ips = [...]
cluster = Cluster(ips)
session = cluster.connect()
session.row_factory = dict_factory
session.set_keyspace("mykeyspace")
response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';")
在输出中,我在单词前面出现了奇怪的“n”,以前是换行符。
示例:
"Over the wintry nforest, winds howl in rage nwith no leaves to blow."
有办法解决这个问题吗?
问题出在您的数据摄取中,或者任何正在消耗 response
而不是驱动程序读取中的问题。它是存储在数据库中的 n
,而不是 \n
字符。 python 驱动程序不会对原始字节进行转义或执行任何操作,而是将其转换为字符串:https://github.com/datastax/python-driver/blob/9869c2a044e5dd76309026437bcda5d01acf99c7/cassandra/cqltypes.py#L693
如果你插入一个 \n
你会得到一个回来:
session.execute('CREATE TABLE mydb (mykey text PRIMARY KEY, mycolumn text);')
session.execute("INSERT INTO mydb (mykey, mycolumn) VALUES ('xyz', 'a\nb');")
response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';")
for line in response:
print line['mycolumn']
正确输出:
a
b
有没有什么东西带走 response
并在之后转义?