简体   繁体   English

转换包含unicode值和浮点数的python列表

[英]Convert a python list having a mix of unicode values and floating numbers

So I am reading values from an excel and storing inside a list. 因此,我正在从Excel中读取值并将其存储在列表中。 But by default, it stored strings as unicode strings and numbers as floats. 但是默认情况下,它将字符串存储为unicode字符串,将数字存储为浮点数。 I want to convert unicode strings into normal strings and float to integer. 我想将unicode字符串转换为普通字符串,然后将其浮动为整数。 How do I do that? 我怎么做?

Here's my list: 这是我的清单:

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

Output: 输出:

[121090999.0, 3454554455.0, u'Shantharam', 121090999.0, 5645.0, u'Puranik']

What I need: 我需要的:

[ 121090999,  3454554455,  'Shantharam', 121090999,  5645,  'Puranik' ]

Below is the list comprehension to convert items in list to: 下面是将list项目转换为的list理解:

  1. str if it is unicode str如果是unicode
  2. int if it is float 如果为float ,则为int
  3. Else, append as it is: 否则,按原样追加:

Sample Example: 范例范例:

>>> my_list = [121090999.0, 3454554455.0, u'Shantharam', 121090999.0, 5645.0, u'Puranik']
>>> [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']

However, instead of converting list to desired format, you should make these checks ( if conditions) at the time you are extending rowvalues 但是, rowvalues将列表转换为所需的格式外,您还应该在扩展rowvalues时进行这些检查( 如果有条件的话)

You can use isinstance to check the type of the variable 您可以使用isinstance检查变量的类型

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']

Here is a short script that should do the trick: 这是一个可以解决问题的简短脚本:

l=[121090999.0, 3454554455.0, u'Shantharam', 121090999.0, 5645.0, u'Puranik'];
i=0
for i in range(len(l)-1):
        if isinstance(l[i], float):
             l[i]=int(l[i])
        else:
             l[i]=str(l[i])
print(l)

Below is the list comprehension code which does exactly what you want: 下面是完全符合您要求的列表理解代码:

   [int(d) if isinstance(d, float) else str(d) if isinstance(d, unicode) else d for d in my_list]

Run this code on your my_list and you'll get the following response: 在您的my_list上运行此代码,您将得到以下响应:

    new_list = [121090999, 3454554455, 'Shantharam', 121090999, 5645, 'Puranik']

You can verify the datatype of the elements as: 您可以通过以下方式验证元素的数据类型:

    type(new_list[-1])  # <type 'str'>
    type(new_list[-2])  # <type 'int'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM