convert unicode type to list or tuple

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hidrkannan
    New Member
    • Feb 2007
    • 32

    convert unicode type to list or tuple

    I am reading a set of data from the excel workbook.
    Data are contained in 2 columns. Column A contains the names and Column B contains values. The issue is the value can be of any type integer, float, string, list or a tuple. When I read the data from a Cell it is read as of data type unicode. I would like to convert the unicode data type to list type if the data I read is of list type and similarly for tuples.

    Please suggest if this could be done.

    Thanks in advance,

    SKN
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    The type conversion can be done, however there's no real way to automatically do it.
    You can certainly do it manually, perhaps with a few try, except clauses.

    int(), float(), list(), tuple(), etc. are there for conversions. If something cannot be converted it will throw an exception so you could catch it and try another conversion, ad nauseum.

    Comment

    • hidrkannan
      New Member
      • Feb 2007
      • 32

      #3
      Originally posted by jlm699
      The type conversion can be done, however there's no real way to automatically do it.
      You can certainly do it manually, perhaps with a few try, except clauses.

      int(), float(), list(), tuple(), etc. are there for conversions. If something cannot be converted it will throw an exception so you could catch it and try another conversion, ad nauseum.

      Thanks jlm699. I tried to parse the object of type unicode. If I convert that using list or tuple, every character is converted to an element to that list or tuple...
      say if I have an object aUnicode = [1,2.0] and if I do aList = list(aUnicode), then aList has [u'[',u'1',u',',u'2 ',u'.',u'0',u']']. It would be even more complex to catch whether if the element is an integer or float.

      I am sure there must be a function to retreive the data from excel to account for the object types...I dont know what it is...

      SKN

      Comment

      • jlm699
        Contributor
        • Jul 2007
        • 314

        #4
        Google the python module xlrd ... it'll take care of data extraction for you.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by hidrkannan
          Thanks jlm699. I tried to parse the object of type unicode. If I convert that using list or tuple, every character is converted to an element to that list or tuple...
          say if I have an object aUnicode = [1,2.0] and if I do aList = list(aUnicode), then aList has [u'[',u'1',u',',u'2 ',u'.',u'0',u']']. It would be even more complex to catch whether if the element is an integer or float.

          I am sure there must be a function to retreive the data from excel to account for the object types...I dont know what it is...

          SKN
          Use eval() to convert. Example:[code=Python]>>> aUnicode = u'[1,2.0]'
          >>> eval(aUnicode)
          [1, 2.0]
          >>> eval(u'1,2,3,4, 5')
          (1, 2, 3, 4, 5)
          >>> eval(u'1.234')
          1.234
          >>> [/code]

          Comment

          • hidrkannan
            New Member
            • Feb 2007
            • 32

            #6
            Originally posted by bvdet
            Use eval() to convert. Example:[code=Python]>>> aUnicode = u'[1,2.0]'
            >>> eval(aUnicode)
            [1, 2.0]
            >>> eval(u'1,2,3,4, 5')
            (1, 2, 3, 4, 5)
            >>> eval(u'1.234')
            1.234
            >>> [/code]
            Thanks again, BV.

            SKN

            Comment

            • hidrkannan
              New Member
              • Feb 2007
              • 32

              #7
              Originally posted by jlm699
              Google the python module xlrd ... it'll take care of data extraction for you.
              Thanks jlm6999. I will look into the xlrd module.

              SKN

              Comment

              • SurestTexas
                New Member
                • Feb 2015
                • 2

                #8
                Went round and round on this too and found your post. The easy way:
                Don't know if this will help YOU out but,
                1) once you have a unicode string, in my case it was because of this code:
                returnedtext = " ".join(cont ent) # this produced a unicode string and I wanted a list -

                2) re-splitting yields the desired list type:
                argsWillBeRetur ned = returnedtext.sp lit(" ")

                Comment

                • SurestTexas
                  New Member
                  • Feb 2015
                  • 2

                  #9
                  Wow, post is OLD (Sorry)

                  Comment

                  Working...