Handling Special characters in python

Discussion in 'Python' started by anilkumar.dannina@gmail.com, Jan 1, 2013.

  1. Guest

    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.

    Waiting for your reply.

    Thanks,
    D Anil Kumar
    , Jan 1, 2013
    #1
    1. Advertising

  2. On Tue, 01 Jan 2013 03:35:56 -0800, anilkumar.dannina 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.



    "endash" is not a character, it is six characters.

    On the other hand, "\x96" is a single byte:

    py> c = u"\x96"
    py> assert len(c) == 1


    But it is not a legal Unicode character:

    py> import unicodedata
    py> unicodedata.name(c)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: no such name


    So if it is not a Unicode character, it is probably a byte.

    py> c = "\x96"
    py> print c
    �


    To convert byte 0x96 to an n-dash character, you need to identify the
    encoding to use.

    (Aside: and *stop* using it. It is 2013 now, anyone who is not using
    UTF-8 is doing it wrong. Legacy encodings are still necessary for legacy
    data, but any new data should always using UTF-8.)

    CP 1252 is one possible encoding, but there may be others:

    py> uc = c.decode('cp1252')
    py> unicodedata.name(uc)
    'EN DASH'



    --
    Steven
    Steven D'Aprano, Jan 1, 2013
    #2
    1. Advertising

  3. Chris Rebert Guest

    On Jan 1, 2013 3:41 AM, <> 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?
    Chris Rebert, Jan 1, 2013
    #3
  4. Guest

    On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:
    > On Jan 1, 2013 3:41 AM, <> wrote:
    >
    > >

    >
    > > I am facing one issue in my module. I am gathering data from sql serverdatabase. 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
    , Jan 2, 2013
    #4
  5. Guest

    On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:
    > On Jan 1, 2013 3:41 AM, <> wrote:
    >
    > >

    >
    > > I am facing one issue in my module. I am gathering data from sql serverdatabase. 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
    , Jan 2, 2013
    #5
  6. Guest

    On Wednesday, January 2, 2013 12:02:34 PM UTC+5:30, Chris Rebert wrote:
    > On Jan 1, 2013 8:48 PM, <> wrote:
    >
    > > On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:

    >
    > > > On Jan 1, 2013 3:41 AM, <> 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 atthe place of "en dash(-)", I am getting '?' symbol. How can I overcome this.
    , Jan 2, 2013
    #6
  7. Guest

    On Wednesday, January 2, 2013 12:02:34 PM UTC+5:30, Chris Rebert wrote:
    > On Jan 1, 2013 8:48 PM, <> wrote:
    >
    > > On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:

    >
    > > > On Jan 1, 2013 3:41 AM, <> 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 atthe place of "en dash(-)", I am getting '?' symbol. How can I overcome this.
    , Jan 2, 2013
    #7
  8. Chris Rebert Guest

    On Wed, Jan 2, 2013 at 5:39 AM, <> wrote:
    > On Wednesday, January 2, 2013 12:02:34 PM UTC+5:30, Chris Rebert wrote:
    >> On Jan 1, 2013 8:48 PM, <> wrote:
    >> > On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:
    >> > > On Jan 1, 2013 3:41 AM, <> 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 tochoose 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
    Chris Rebert, Jan 3, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    33,008
    Stefan Mueller
    Jul 23, 2006
  2. Replies:
    1
    Views:
    1,573
    Greg R. Broderick
    Apr 5, 2007
  3. Replies:
    2
    Views:
    1,089
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,651
    rvino
    Aug 14, 2007
  5. majna
    Replies:
    4
    Views:
    665
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page