473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 10 '08 #1
8 25718
jlm699
314 100+
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
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
314 100+
Google the python module xlrd ... it'll take care of data extraction for you.
Jul 10 '08 #4
bvdet
2,851 Expert Mod 2GB
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
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
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
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
Wow, post is OLD (Sorry)
Feb 16 '15 #9

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

Similar topics

17
by: Pierre Fortin | last post by:
Hi, Is the following a reasonable generic approach for converting python returned tuples/lists into dicts...? I'm not advocating library functions also return dicts (I'd probably spend more...
7
by: querypk | last post by:
how do I convert b is a string b = '(1,2,3,4)' to b = (1,2,3,4)
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
6
by: Aneesh E Warrier | last post by:
How can I convert Unicode Codepoint (dec) value to ASCII char? For example: 49324 is 사 a Korean alphabet, and I want to convert it to ASCII letter. Thanks!
25
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to...
9
by: vaskar | last post by:
I have got one problem in VB.NET 2005 using unicode I want to add two numbers in vb.net 2005 through UNICODE I didn't know how to convert UNICODE string to integer. I hope you can solve my...
0
by: M.-A. Lemburg | last post by:
On 2008-07-01 20:31, Peter Bulychev wrote: You could write a codec which translates Unicode into a ASCII lookalike characters, but AFAIK there is no standard for doing this. I guess the best...
3
by: bruce | last post by:
hi guys/gals... got a basic question that i can't get my hands around. i'm trying to programatically create/use a list/tuple (or whatever the right phrase in pyton is!!) basically,...
0
by: bruce | last post by:
Hi Fredrik... so, this still doesn't get me an array called 'cat', or 'dog' or do i somehow use stuff to add/extract the vals...??? thanks -----Original Message-----
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.