Handling Special characters in python

Chris Rebert clp2 at rebertia.com
Thu Jan 3 01:00:16 EST 2013


On Wed, Jan 2, 2013 at 5:39 AM,  <anilkumar.dannina at gmail.com> wrote:
> On Wednesday, January 2, 2013 12:02:34 PM UTC+5:30, Chris Rebert wrote:
>> On Jan 1, 2013 8:48 PM, <anilkuma... at gmail.com> wrote:
>> > On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:
>> > > On Jan 1, 2013 3:41 AM, <anilkuma... at gmail.com> wrote:
>> > > > I am facing one issue in my module. I am gathering data from sql server database. In the data that I got from db contains special characters like "endash". Python was taking it as "\x96". I require the same character(endash). How can I perform that. Can you please help me in resolving this issue.
>>
>> > > 1. What library are you using to access the database?
>> > > 2. To confirm, it's a Microsoft SQL Server database?
>> > > 3. What OS are you on?
>>
>> > 1. I am using "pymssql" module to access the database.
>> > 2. Yes, It is a SQL server database.
>> > 3. I am on Ubuntu 11.10
>>
>> Did you set "client charset" (to "UTF-8", unless you have good reason to choose otherwise) in freetds.conf? That should at least ensure that the driver itself is exchanging bytestrings via a well-defined encoding.
>> If you want to work in Unicode natively (Recommended), you'll probably need to ensure that the columns are of type NVARCHAR as opposed to VARCHAR. Unless you're using SQLAlchemy or similar (which I personally would recommend using), you may need to do the .encode() and .decode()-ing manually, using the charset you specified in freetds.conf.
>>
>> Sorry my advice is a tad general. I went the alternative route of SQLAlchemy + PyODBC + Microsoft's SQL Server ODBC driver for Linux (http://www.microsoft.com/en-us/download/details.aspx?id=28160 ) for my current project, which likewise needs to fetch data from MS SQL to an Ubuntu box. The driver is intended for Red Hat and isn't packaged nicely (it installs via a shell script), but after that was dealt with, things have gone smoothly. Unicode, in particular, seems to work properly.
>
> Thanks Chris Rebert for your suggestion, I tried with PyODBC module, But at the place of "en dash(-)", I am getting '?' symbol. How can I overcome this.

I would recommend first trying the advice in the initial part of my
response rather than the latter part. The latter part was more for
completeness and for the sake of the archives, although I can give
more details on its approach if you insist.

Additionally, giving more information as to what exactly you tried
would be helpful. What config / connection settings did you use? Of
what datatype is the relevant  column of the table? What's your code
snippet look like? Etc..

Regards,
Chris



More information about the Python-list mailing list