Python xlrd 读取为字符串 [英] Python xlrd read as string

查看:137
本文介绍了Python xlrd 读取为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xlrd 中从 Excel 读取特定单元格值时遇到困难.我正在阅读的任何值(日期值)都被转换为一个数字.我知道有将其转换为python日期格式的解决方案,但是我可以直接读取xlrd中的字符串值吗?

I'm having difficulties in reading a particular cell value from Excel in xlrd. Whatever value I'm reading (date value) is getting converted to a number. I know there are solutions to convert it into a python date format, but can I read directly the string value in xlrd?

推荐答案

xlrd 不会将日期转换为浮点数.Excel 将日期存储为浮点数.

xlrd does NOT convert dates to float. Excel stores dates as floats.

引用 xlrd 文档(向下滚动页面):

Quoting from the xlrd documentation (scroll down a page):

Excel 电子表格中的日期

实际上,没有这样的事情.你所拥有的是浮点数数字和虔诚的希望.有Excel 日期的几个问题:

In reality, there are no such things. What you have are floating point numbers and pious hope. There are several problems with Excel dates:

(1) 日期不作为单独的数据类型;它们存储为浮动点数,你必须依靠(a) 适用于的数字格式"他们在 Excel 中和/或 (b) 知道哪个单元格应该有日期他们.该模块有助于 (a) 通过检查已设置的格式应用于每个数字单元格;如果它似乎是日期格式,单元格被归类为日期而不是号.

(1) Dates are not stored as a separate data type; they are stored as floating point numbers and you have to rely on (a) the "number format" applied to them in Excel and/or (b) knowing which cells are supposed to have dates in them. This module helps with (a) by inspecting the format that has been applied to each number cell; if it appears to be a date format, the cell is classified as a date rather than a number.

(2) ...当使用这个包的xldate_as_tuple() 函数进行转换工作簿中的数字,您必须使用 datemode 属性Book 对象.

(2) ... When using this package’s xldate_as_tuple() function to convert numbers from a workbook, you must use the datemode attribute of the Book object.

另请参阅关于 Cell 类 了解单元格的类型,以及各种 Sheet 方法 提取单元格的类型(文本、数字、日期、布尔值等).

See also the section on the Cell class to learn about the type of cells, and the various Sheet methods which extract the type of a cell (text, number, date, boolean, etc).

查看 python-excel.org 以获取有关其他 Python Excel 包的信息.

Check out python-excel.org for info on other Python Excel packages.

这篇关于Python xlrd 读取为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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