【问题标题】:Converting CSV to sqlite3 table in pandas - "8-bit bytestrings" error在 Pandas 中将 CSV 转换为 sqlite3 表 - “8 位字节串”错误
【发布时间】:2018-05-05 09:55:24
【问题描述】:

我正在尝试将表加载到 sqlite3。我目前有 csv 格式。 表格有多种格式——文本、数字、日期、NaN...

代码如下:

import pandas as pd
import sqlite3
df = pd.read_csv("filename.csv")
conn = sqlite3.connect('test.db')
df.to_sql('file', conn, flavor='sqlite', if_exists='replace', index=False)

我收到以下错误:

ProgrammingError: 不得使用 8 位字节串,除非您使用可以解释 8 位字节串的 text_factory(如 text_factory = str)。强烈建议您将应用程序切换为 Unicode 字符串

数据库和sqlite表都不存在。我想将 csv 转换为文件。

【问题讨论】:

    标签: python pandas csv sqlite


    【解决方案1】:

    通过向 read_csv 部分添加编码解决了我的问题。

    df = pd.read_csv("filename.csv", encoding='latin-1')
    

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 2020-04-08
      • 1970-01-01
      • 2016-11-05
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      相关资源
      最近更新 更多