Skip to main content
 首页 » 编程设计

python之在python中使用xlrd从xls中提取数据

2025年05月04日1598°冷暖

我正在尝试从 .xls 文件中提取数据 并制作一个列表,但我得到的列表为 [u'elem1', u'elem2', u'elem3 '],但如果我单独打印,我会得到:

elem1 
elem2 
elem3 

那是什么东西,如何移除它?

这是我的代码...

from xlrd import open_workbook 
xls=open_workbook('name.xls') 
for sheets in xls.sheets(): 
    list1=[] 
    for col in range(sheets.ncols): 
        for rows in range(sheets.nrows): 
            list1.append(sheets.cell(rows, col).value) 
print(list1) 
for i in list1: 
    print(i) 

请您参考如下方法:

您可以将文本定义为字符串,同时将数据附加到 list1.append(str(sheets.cell(rows, col).value)) 中的列表中以删除 [u ' 。代码将是:

   from xlrd import open_workbook 
   xls=open_workbook('name.xls') 
   for sheets in xls.sheets(): 
       list1=[] 
       for col in range(sheets.ncols): 
           for rows in range(sheets.nrows): 
               list1.append(str(sheets.cell(rows, col).value)) 
       print(list1) 
       for i in list1: 
           print i