【问题标题】:PyMySQL with Python 3.5 - selecting into pandas dataframe with the LIKE clause fails due to escape characters?使用 Python 3.5 的 PyMySQL - 由于转义字符,使用 LIKE 子句选择 Pandas 数据帧失败?
【发布时间】:2018-02-11 13:47:25
【问题描述】:

我正在使用 PyMySQL 将一些数据从 MySQL DB 提取到 pandas 数据帧中。我需要使用 LIKE 子句运行选择,但似乎 PyMySQL 对 select 语句做了一些奇怪的事情,并且不喜欢在查询中有 % 时:

#connection to MySQL
engine = create_engine('mysql+pymysql://user:password@localhost:1234/mydb', echo=False)
#get descriptions we want
decriptions = pd.read_sql(sql=r"select content from listings where content not like '%The Estimate%'", con = engine)

我得到错误:

ValueError: unsupported format character 'T' (0x54) at index 54

关于如何解决这个问题的任何建议?

【问题讨论】:

  • @cᴏʟᴅsᴘᴇᴇᴅ 我已经试过了 - 没有变化

标签: python mysql pandas python-3.5


【解决方案1】:

尝试使用%%

decriptions = pd.read_sql(sql=r"select content 
         from listings where content not like '%%The Estimate%'", con = engine)

【讨论】:

  • 谢谢先生。您能否提供有关为什么/发生了什么的背景?
  • 因为 pyton 使用 % 作为参数 .. 所以你需要为 like 添加 %
  • 我明白了,但关键是我使用 SQLAlchemy 进行连接,当我连接到 MSSQL 服务器时,此查询在没有 %% 使用率的情况下工作正常。所以我只是想通过 PyMySQL lib 理解为什么 MmySQL 需要它
  • 我无法帮助您解决两个 DB 驱动程序之间的差异
猜你喜欢
  • 2019-09-29
  • 1970-01-01
  • 2016-04-21
  • 2018-04-18
  • 1970-01-01
  • 2015-04-23
  • 2014-10-31
  • 1970-01-01
  • 2016-06-19
相关资源
最近更新 更多