46402

Handling accents in Oracle from Python

Question:

I have the following code:

#!/usr/bin/python # coding=UTF-8 import cx_Oracle def oracle_connection(user, passwd, host, port, service): oracle_con_details = user+'/'+passwd+'@'+host+':'+port+'/'+service try: oracle_connection = cx_Oracle.connect(oracle_con_details) except cx_Oracle.DatabaseError as e: error, = e.args if error.code == 1017: log.warning('Please check your credentials.') else: log.error('Database connection error: ') log.error(e) return oracle_connection user_oracle = "user" passw_oracle = "pass" host_oracle = "host" port_oracle = "port" service_oracle = "service" con_oracle = oracle_connection(user_oracle, passw_oracle, host_oracle, port_oracle, service_oracle) query = """ SELECT COUNT(*) FROM TABLE WHERE MYDATA = 'REUNIÓN'""" cursor_oracle = con_oracle.cursor() cursor_oracle.execute(query) data_tuple = cursor_oracle.fetchall()

Of course, Oracle credentials and query are just examples. Notice the query has 'Ó' character. This is the one giving me the following error:

<blockquote>

UnicodeEncodeError: 'ascii' codec can't encode character u'\xd3' in position 49: ordinal not in range(128)

</blockquote>

I've tried the solutions from some other questions here:

query.decode('utf-8') query.encode('utf-8') query.decode('unicode') query.encode('unicode')

I understand my string (query) is encoded in unicode but I just don't understand why decoding it in utf-8 doesn't work.

Because of this my query doesn't get to Oracle how it should.

<strong>ADDITIONAL INFO:</strong>

Based on <a href="http://So%20your%20variable%20either%20contains%20a%20unicode%20string%20or%20a%20byte%20string.%20If%20it%20is%20a%20unicode%20string%20you%20can%20just%20do%20appName.encode(%22utf-8%22)." rel="nofollow noreferrer">this</a> answer I thought mystring.encode('utf-8) would work.

I cheked the type of my string with <a href="https://stackoverflow.com/questions/4987327/how-do-i-check-if-a-string-is-unicode-or-ascii" rel="nofollow">this</a> method just in case and the result is 'ordinary string'.

Answer1:

Adding this to my python code solved it.

import os os.environ["NLS_LANG"] = "SPANISH_SPAIN.UTF8"

Recommend

  • How to use JNI to call a “main” function that takes an array of character pointers?
  • Escape exclamation mark in batch file
  • Passing argument within action selection in UIButton
  • Why are pure reducers so important in redux?
  • Permit array in params
  • Putting incomplete nested lists in a rectangular ndarray
  • Control a pager (like the less program) from an extenal (parent) script
  • ManagementObjectSearcher select network printers?
  • Close Colorbox iframe after form submit and then redirect parent page
  • complex annotate on django query set
  • Passing array length to function
  • Inno Setup Search for specifc file on a CD, retrieve exact filepath and return value to [Files]-Sect
  • How can I include multiple models in one view for in a Joomla 3.x component built with Component Cre
  • Detaching entity along with referenced entities
  • Authentication - JavaScript - Logout issue
  • Sending progress value to progress bar in python
  • TCPDF's getNumLines() is sometimes wrong
  • In VIm, how to remove all lines that are duplicate somewhere?
  • C# “cannot assign field because it is a foreach iteration variable”
  • How to startActivity for Result from Activity under Group Activity Under Tabs?
  • SCons libraries and sub-libraries
  • How Can I Prevent Activation For Some ListView Items When The Selection Mode Is MultiChoiceModal?
  • How to configure a timeout for an SQL query in Groovy?
  • coldfusion variables into powershell
  • Use allowDiskUse in criteria query with Grails and the MongoDB plugin?
  • API (curl)Command to Approve a promoted build Job in Jenkins
  • passing a javascript variable to PHP with xmlhttprequest
  • C++ Pointer Arrays
  • How to protect an asp:textbox from user input?
  • Wait for .each() .getJSON request to finish before executing a callback
  • passing parameter to DownloadStringCompletedEventHandler in C#
  • Configuring multiple DefaultJmslistenercontainerfactory
  • How to merge keras sequential models with same input?
  • (Tcl/Expect) clear screen after exit
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • What command do i need to pass in SabreCommandLLSRQ to get current price of PNR?
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Creating a DropDownList
  • Who propagate bugfixes across branches (corporate development)?
  • Scrapy recursive link crawler