【问题标题】:Error in reading an sql file using pandas使用 pandas 读取 sql 文件时出错
【发布时间】:2017-07-29 01:56:38
【问题描述】:

我有一个本地存储在我的 PC 中的 sql 文件。我想使用 pandas 库打开并阅读它。这是我尝试过的:

 import pandas as pd
 import sqlite3

 my_file = 'C:\Users\me\Downloads\\database.sql'
 #I am creating an empty database
 conn = sqlite3.connect(r'C:\Users\test\Downloads\test.db')
 #I am reading my file 
 df = pd.read_sql(my_file, conn)

但是,我收到以下错误:

DatabaseError:在 sql 'C:\Users\me\Downloads\database.sql' 上执行失败:“C”附近:语法错误

【问题讨论】:

  • 试试my_file = r'C:\Users\me\Downloads\\database.sql',这样你就可以传递一个原始字符串,或者使用正斜杠
  • @EdChum 没用 :(
  • 去掉双 \\ 并在 .sql 路径中改为单 \,使用 EdChum 之前所说的原始字符串编码
  • my_file = 'C://Users/me/Downloads/database.sql'怎么样
  • 为了详细说明@EdChum 的评论,Python 将尝试将'C:\Users''\U' 之后的所有内容转换为unicode,这将破坏“路径字符串”。绝对使用r 来表示它是一个原始字符串。另见:en.wikipedia.org/wiki/…

标签: python sql pandas


【解决方案1】:

尝试将文件移动到 D:// 有时 Python 没有被授予在 C 中读/写的权限。 因此,这可能是一个问题。

您也可以尝试使用光标的替代方法。

cur=conn.cursor()

r=cur.fetchall()

此 r 将包含您的数据集的元组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2021-11-08
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2021-07-04
    相关资源
    最近更新 更多