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 <code>list</code> comprehension to convert items in list to:</p>

<ol>
<li><code>str</code> if it is <code>unicode</code></li>
<li><code>int</code> if it is <code>float</code></li>
<li>Else, append as it is:</li>
</ol>

<blockquote>
  <p>Sample Example:</p>
</blockquote>

<pre><code>&gt;&gt;&gt; my_list = [121090999.0, 3454554455.0, u'Shantharam', 121090999.0, 5645.0, u'Puranik']
&gt;&gt;&gt; [str(item) if isinstance(item, unicode) else int(item) if isinstance(item,float) else item for item in my_list]
# Output: [121090999, 3454554455, 'Shantharam', 121090999, 5645, 'Puranik']
</code></pre>

<p>However, instead of converting list to desired format, you should make these checks (<em>if</em> conditions) at the time you are extending <code>rowvalues</code></p>

python unicode xlrd