2 Replies Latest reply on Jun 11, 2015 2:14 PM by Timothy Lombard

    Is this the best way to cope with unicode (Python Dataextract)?

    Timothy Lombard

      So I just got my Python 2.7.8  environment configured to have the Tableau DataExtract API loaded on my Linux machine. 

      I'm pulling project description information from an XML web-services response and stuffing the info into a row with the following command:

      milerow.setCharString(8, ppm_pdict[p].description)


      The following error get's thrown:

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


      I searched stackoverflow.com and found hints for the solution below from User Martijn Pieters - Stack Overflow


      #UNICODE is a pain in my ****! below is a function to strip-out the unicode chars and replace with a space.

      def remove_non_ascii(text):

          return ''.join([i if ord(i) < 128 else ' ' for i in text])


      Then updated the row stuff command:

      milerow.setCharString(8, remove_non_ascii(ppm_pdict[p].description))


      The script runs now but the project description is mutated-  Is there a better way?