Convert a python list having a mix of unicode values and floating numbers
<p>So I am reading values from an excel and storing inside a list. But by default, it stored strings as unicode strings and numbers as floats. I want to convert unicode strings into normal strings and float to integer. How do I do that?</p>

<p>Here's my list:</p>

<pre><code>import xlrd 
wb = xlrd.open_workbook("test.xlsx")
firstsheet = wb.sheet_by_index(0)
rows =1
rowvalues = []
while(rows &lt;= (firstsheet.nrows)-1):
     rowvalues.extend(firstsheet.row_values(rows)) 
     rows+=1
print rowvalues
</code></pre>

<p>Output:</p>

<pre><code>[121090999.0, 3454554455.0, u'Shantharam', 121090999.0, 5645.0, u'Puranik']
</code></pre>

<p>What I need:</p>

<pre><code>[ 121090999,  3454554455,  'Shantharam', 121090999,  5645,  'Puranik' ]
</code></pre>

<p>Below is the list comprehension code which does exactly what you want:</p>

<pre><code>   [int(d) if isinstance(d, float) else str(d) if isinstance(d, unicode) else d for d in my_list]
</code></pre>

<p>Run this code on your <code>my_list</code> and you'll get the following response:</p>

<pre><code>    new_list = [121090999, 3454554455, 'Shantharam', 121090999, 5645, 'Puranik']
</code></pre>

<p>You can verify the datatype of the elements as:</p>

<pre><code>    type(new_list[-1])  # &lt;type 'str'&gt;
    type(new_list[-2])  # &lt;type 'int'&gt;
</code></pre>

python unicode xlrd