我正在尝试使用Pandas执行SqlLite的REGEXP函数,但得到的错误是
"ImportError:使用未安装sqlalchemy的URI字符串。
python代码如下:
import pandas as pd
import csv, sqlite3
import json, re
conn = sqlite3.connect(":memory:")
print(sqlite3.version)
print(sqlite3.sqlite_version)
def regexp(y, x, search=re.search):
return 1 if search(y, x) else 0
conn.create_function("regexp", 2, regexp)
df = pd.read_json("idxData1.json", lines=True)
df.to_sql("temp_log", conn, if_exists="append", index=False)
rsDf = pd.read_sql_query(
conn, """SELECT * from temp_log WHERE user REGEXP 'ph'""", chunksize=20,
)
for gendf in rsDf:
for item in gendf.to_dict(orient="records"):
print(item)
它抛出的错误是
raise ImportError("Using URI string without sqlalchemy installed.")
ImportError: Using URI string without sqlalchemy installed.
有人能告诉我我错过了什么吗。请不要说我有使用Pandas的具体要求。
发布于 2022-07-16 18:41:31
您得到此错误是因为您以错误的顺序将参数指定给read_sql_query
。具体来说,第一个参数应该是查询,连接排在第二位,如下所示:
rsDf = pd.read_sql_query(
"""SELECT * from temp_log WHERE user REGEXP 'ph'""", conn, chunksize=20,
)
发布于 2021-01-23 06:54:36
只需运行以下命令并安装SQLAlchemy
即可
pip3 install SQLAlchemy
https://stackoverflow.com/questions/64319021
复制相似问题