简体   繁体   English

UnicodeEncodeError: 在 python 中使用 to_sql 时

[英]UnicodeEncodeError: while using to_sql in python

I am trying to insert dataframe to oracle database using to_sql.我正在尝试使用 to_sql 将数据帧插入到 oracle 数据库中。 Below is the code:下面是代码:

engine = create_engine('oracle+cx_oracle://'+username+':'+password+'@'+host+':'+port+'/'+sid) df.to_sql(name = table.lower(),schema=schema,con =engine,if_exists = 'replace', index=False) engine = create_engine('oracle+cx_oracle://'+username+':'+password+'@'+host+':'+port+'/'+sid) df.to_sql(name = table.lower(),schema=schema ,con =engine,if_exists = 'replace', index=False)

I am getting below error: UnicodeEncodeError: 'ascii' codec can't encode character '\–' in position 81: ordinal not in range(128)我收到以下错误:UnicodeEncodeError: 'ascii' codec can't encode character '\–' in position 81: ordinal not in range(128)

can someone please help on this.有人可以帮忙吗?

Probably your database is UTF8 and you did not export your NLS_LANG variable in your session:可能您的数据库是 UTF8,并且您没有在会话中导出 NLS_LANG 变量:

So I would try to first export your NLS_LANG to the character set you have in your database:所以我会尝试首先将您的 NLS_LANG 导出到您数据库中的字符集:

#export NLS_LANG=American_America.AL32UTF8

Then I would try to change your connection in cx_Oracle.connect method:然后我会尝试在 cx_Oracle.connect 方法中更改您的连接:

connection = cx_Oracle.connect("user/password@connectString",
    encoding="UTF-8", nencoding="UTF-8")

As you are using to_sql , you might try this before you try your connection当您使用 to_sql 时,您可以在尝试连接之前尝试此操作

import os
os.environ["NLS_LANG"] = 'YOUR_NLS_LANG_VARIABLE'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM