By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

convert unicode type to list or tuple

P: 32
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
Jul 10 '08 #1
Share this Question
Share on Google+
8 Replies


jlm699
100+
P: 314
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.
Jul 10 '08 #2

P: 32
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
Jul 10 '08 #3

jlm699
100+
P: 314
Google the python module xlrd ... it'll take care of data extraction for you.
Jul 10 '08 #4

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. >>> aUnicode = u'[1,2.0]'
  2. >>> eval(aUnicode)
  3. [1, 2.0]
  4. >>> eval(u'1,2,3,4,5')
  5. (1, 2, 3, 4, 5)
  6. >>> eval(u'1.234')
  7. 1.234
  8. >>> 
Jul 10 '08 #5

P: 32
Use eval() to convert. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> aUnicode = u'[1,2.0]'
  2. >>> eval(aUnicode)
  3. [1, 2.0]
  4. >>> eval(u'1,2,3,4,5')
  5. (1, 2, 3, 4, 5)
  6. >>> eval(u'1.234')
  7. 1.234
  8. >>> 
Thanks again, BV.

SKN
Jul 10 '08 #6

P: 32
Google the python module xlrd ... it'll take care of data extraction for you.
Thanks jlm6999. I will look into the xlrd module.

SKN
Jul 10 '08 #7

P: 2
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(content) # this produced a unicode string and I wanted a list -

2) re-splitting yields the desired list type:
argsWillBeReturned = returnedtext.split(" ")
Feb 16 '15 #8

P: 2
Wow, post is OLD (Sorry)
Feb 16 '15 #9

Post your reply

Sign in to post your reply or Sign up for a free account.