Python xlrd:如何转换提取的值? [英] Python xlrd : how to convert an extracted value?

查看:31
本文介绍了Python xlrd:如何转换提取的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个问题,根据我在此处找到的内容,我觉得我已多次得到解答.但是,作为新手,我无法真正理解如何执行真正的基本操作.

事情是这样的:

  • 我有一个 .xls 并且当我使用 xlrd 获取一个值时,我只是在使用sh.cell(0,0)(假设sh是我的sheet);

  • 如果单元格中的内容是字符串,我会得到类似 text:u'MyName' 的内容,我只想保留字符串 'MyName';

  • 如果单元格中的内容是一个数字,我会得到类似 number:201.0 的东西,我只想保留整数 201.

如果有人能告诉我我应该如何只提取我想要的格式的值,谢谢.

解决方案

sh.cell(x, y) 返回类 Cell 的一个实例.当您打印 sh.cell(x,y) 时,您将返回该类的 repr 函数(因此它打印 type:value).

你应该尝试:

cell = sh.cell(x,y)打印(单元格值)

我无法测试这个,因为我没有 xlrd 但是,我认为它会在文档中起作用:https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#sheet.Cell-class>

Well i have a question that i feel i've been answered several times, from what i found here. However, as a newbie, i can't really understand how to perform a really basic operation.

Here's the thing :

  • i have an .xls and when i use xlrd to get a value i'm just using sh.cell(0,0) (assuming that sh is my sheet);

  • if what is in the cell is a string i get something like text:u'MyName' and i only want to keep the string 'MyName';

  • if what is in the cell is a number i get something like number:201.0 and i only want to keep the integer 201.

If anyone can indicate me what i should to only extract the value, formatted as i want, thank you.

解决方案

sh.cell(x, y) returns an instance of the class Cell. When you print sh.cell(x,y) you are returning the repr function of the class (so it prints type:value).

you should try:

cell = sh.cell(x,y)
print(cell.value)

I cannot test this since I don't have xlrd but, I think it will work given the documentation: https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#sheet.Cell-class

这篇关于Python xlrd:如何转换提取的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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