首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ImportError:在未安装sqlalchemy的情况下使用URI字符串,同时使用SqlLite在pandas上执行REGEXP函数。

ImportError:在未安装sqlalchemy的情况下使用URI字符串,同时使用SqlLite在pandas上执行REGEXP函数。
EN

Stack Overflow用户
提问于 2020-10-12 13:39:32
回答 2查看 4.4K关注 0票数 0

我正在尝试使用Pandas执行SqlLite的REGEXP函数,但得到的错误是

"ImportError:使用未安装sqlalchemy的URI字符串。

python代码如下:

代码语言:javascript
运行
复制
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)

它抛出的错误是

代码语言:javascript
运行
复制
   raise ImportError("Using URI string without sqlalchemy installed.")
ImportError: Using URI string without sqlalchemy installed.

有人能告诉我我错过了什么吗。请不要说我有使用Pandas的具体要求。

EN

回答 2

Stack Overflow用户

发布于 2022-07-16 18:41:31

您得到此错误是因为您以错误的顺序将参数指定给read_sql_query。具体来说,第一个参数应该是查询,连接排在第二位,如下所示:

代码语言:javascript
运行
复制
rsDf = pd.read_sql_query(
    """SELECT * from temp_log WHERE user REGEXP 'ph'""", conn, chunksize=20,
)
票数 1
EN

Stack Overflow用户

发布于 2021-01-23 06:54:36

只需运行以下命令并安装SQLAlchemy即可

代码语言:javascript
运行
复制
pip3 install SQLAlchemy
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64319021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档