在python中使用xlrd从xls提取数据 [英] data extraction from xls using xlrd in python

查看:104
本文介绍了在python中使用xlrd从xls提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to extract the data from an .xls file and making a list but i am getting the list as [u'elem1', u'elem2', u'elem3'], but if i print separately i get as:

elem1
elem2
elem3

这是什么东西,以及如何将其删除?

what is that u thing and how to remove it?

这是我的代码...

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))中的列表中.值)删除 [u'.代码为:

You can define the text as string,while appending data to the list in list1.append(str(sheets.cell(rows, col).value)) to remove [u' .The code will be:

   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

这篇关于在python中使用xlrd从xls提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆