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>You can use <a href="https://docs.python.org/2/library/functions.html#isinstance" rel="nofollow">isinstance</a> to check the type of the variable</p>

<pre><code>rows = [121090999.0, 3454554455.0, u'Shantharam', 121090999.0, 5645.0, u'Puranik']
def convert_rows(rows):
    for kk,vv in enumerate(rows):        
        if isinstance(vv,float):
            rows[kk] = int(vv)
        if isinstance(vv,unicode):
            rows[kk] = vv.encode('ascii')
    return rows
print( convert_rows(rows) )
# prints [121090999, 3454554455L, 'Shantharam', 121090999, 5645, 'Puranik']
</code></pre>

python unicode xlrd