① 如何使用python在hbase里進行模糊查詢
for key, data in graph_table.scan(filter="SingleColumnValueFilter('cf', 'id', <, 'binary:%s', true, false)" % struct.pack(">q", 1000)):
print key, data
這個語句是查詢id<1000的,你改一下吧。。。。真心沒用過HBase
② python SQL模糊查詢語句問題
在Python上如果使用sql語句:
select * from table_name where field_name like '%上海%';
執行的時候會出現pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax)的錯誤,這個是因為模糊查詢使用like %%出現了問題。
解決:
字元串使用%需要轉義,將sql語句改為:
select * from table_name where field_name like '%%上海%%';即可。
③ 如何使用python在hbase里進行模糊查詢
這兩天正好在做和題主一樣的事情,剛開始在網上找資料還看到了這個問題,現在稍微明白了,雖然是很久之前的問題了,回來強答一下;
注意:正則的寫法可能不對,保證能過濾出數據,但是可能不會嚴格匹配,正則問題請自己解決;
#導入thrift和habse包
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
#此處可以修改地址和埠
host = '192.168.1.1'
#默認埠為9090
port = 9090
#要查詢的表名
table = 'table_name'
#定義一個過濾器,此為關鍵步驟
filter = "RowFilter(=,'regexstring:.3333.')" #此行原創:)
# Make socket
transport = TSocket.TSocket(host, port)
# Buffering is critical. Raw sockets are very slow
# 還可以用TFramedTransport,也是高效傳輸方式
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
#傳輸協議和傳輸過程是分離的,可以支持多協議
protocol = TBinaryProtocol.TBinaryProtocol(transport)
#客戶端代表一個用戶
client = Hbase.Client(protocol)
#打開連接
try:
transport.open()
scan.filterString=filter
scanner = client.scannerOpenWithScan(table, scan)
except Exception:
finally:
client.sc
④ 如何使用python在hbase里進行模糊查詢
#導入thrift和habse包
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
#此處可以修改地址和埠
host = '192.168.1.1'
#默認埠為9090
port = 9090
#要查詢的表名
table = 'table_name'
#定義一個過濾器,此為關鍵步驟
filter = "RowFilter(=,'regexstring:.3333.')" #此行原創:)
# Make socket
transport = TSocket.TSocket(host, port)
# Buffering is critical. Raw sockets are very slow
# 還可以用TFramedTransport,也是高效傳輸方式
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
#傳輸協議和傳輸過程是分離的,可以支持多協議
protocol = TBinaryProtocol.TBinaryProtocol(transport)
#客戶端代表一個用戶
client = Hbase.Client(protocol)
#打開連接
try:
transport.open()
scan.filterString=filter
scanner = client.scannerOpenWithScan(table, scan)
except Exception:
finally:
client.scannerClose(scan)
transport.close()
連接代碼網上一搜一大堆,非原創,來源已不可考,非本人研究成果;
關鍵就是這個:"RowFilter(=,'regexstring:.3333.')"
這個過濾器要寫對,hbase有十幾種內置的過濾器方法,有幾種比較運算符和比較器,上面這個是正則方式,即'regexstring:.3333.';
過濾器整個雙引號裡面的內容會通過thrift傳給hbase服務端處理,下劃線這部分正則要支持java的正則要求不然會報錯
⑤ python 模糊查詢
字元串有個function叫startswith
根據你的內容我提供一段代碼
for str in list:
if str.startswith('a'):
print "found it!",str
樓主測試下哈,我用的是2.x。
有問題再聯系我~~蟒蛇小組祝您編程愉快!
⑥ 我在學習python編程,請問下怎麼實現模糊查詢
importre
f=open('user.txt','r')
text=f.read()
f.close()
tofind=raw_input("pleaseinputyowanttofind:")
tofind=re.escape(tofind)
result=re.findall(".*"+tofind+".*",text)
forlineinresult:printline
⑦ python 簡單模糊匹配
根據報錯的信息find這個變數是float類型而不是str類型的,str才有startsWith這個方法,你想找的實際上是excel表格中的值,我覺得你需要先把find這個變數在後台列印出來,如以下代碼
forfindinxx:
print"@54",find
iffind.startswith('A1'):
....
...
⑧ python sqlite3 如何模糊查詢變數
剛剛研究了一下,我的代碼是在python 3.2.3下的。不知你的版本是多少,姑且參考吧。 以下代碼根據python的手冊里的例子改編。import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() cur.execute("create table people (name, age)") who = "Yeltsin"age = 72# This is the qmark style: cur.execute("insert into people values (?, ?)", (who, age)) # Query using like clause cur.execute("select * from people where name like ?",('%Yel%',))。