【问题标题】:Pandas to SQL (via SQL Alchemy) yields Unicode Encode ErrorPandas to SQL(通过 SQL Alchemy)产生 Unicode 编码错误
【发布时间】:2016-04-20 20:48:57
【问题描述】:

我有这些 CSV 文件(从here 指向 zip 文件的顶部链接),我正试图将它们插入 Oracle 数据库。 使用以下代码时出现此错误。我尝试使用 utf-8 进行编码,但没有骰子...

UnicodeEncodeError: 'ascii' codec can't encode character '\xdf' in position 18: 
ordinal not in range(128)

正在使用的代码:

import pandas as pd
from sqlalchemy import create_engine
import cx_Oracle as cx
pwd=input('Enter Password for server:')
engine = create_engine('oracle+cx_oracle://schema:'+pwd+'@server:1521/service_name')
df=pd.read_csv(r'path\data.csv',encoding='latin-1',index_col=0)
name='table1'
df.to_sql(name,engine,if_exists='append')

当我通过 pd.read_csv 打开 csv 文件时,它在我的 Jupyter 笔记本中运行良好。

【问题讨论】:

    标签: oracle python-3.x pandas unicode


    【解决方案1】:

    看来我只需要在 create_engine 中使用 encoding='latin-1' 参数。

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 2020-05-23
      • 2020-11-11
      • 1970-01-01
      • 2019-05-06
      • 2010-12-12
      相关资源
      最近更新 更多