Python xlrd 读取为字符串

发布于 2024-11-02 09:52:09 字数 109 浏览 0 评论 0原文

我在从 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亚希 2024-11-09 09:52:09

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

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

Excel 电子表格中的日期

事实上,并不存在这样的事情。
你拥有的是浮点
数字和虔诚的希望。有
Excel 日期的几个问题:

(1) 日期不单独存储
数据类型;它们存储为浮动
点数,你必须依赖
(a) 适用的“数字格式”
他们在 Excel 中和/或 (b) 知道哪些
单元格应该有日期
他们。该模块通过以下方式帮助实现 (a)
检查已经存在的格式
应用于每个数字单元格;如果它
似乎是日期格式,单元格
被分类为日期而不是
数量。

(2) ... 使用此包的 < code>xldate_as_tuple() 函数进行转换
工作簿中的数字,您必须使用的 datemode 属性
Book 对象。

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

查看 python-excel.org 了解有关其他 Python Excel 包的信息。

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

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

Dates in Excel spreadsheets

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) 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) ... 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.

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).

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

如梦初醒的夏天 2024-11-09 09:52:09

好吧,正如您所说:

# reading from a xls file (no .xlsx files, no writing!)
import xlrd  # install xlrd from  http://pypi.python.org/pypi/xlrd

wb = xlrd.open_workbook("YOUR_FILE.xls")  # xls file to read from
sh1 = wb.sheet_by_index(0) # first sheet in workbook
sh2 = wb.sheet_by_name('colors') # sheet called colors

# print all rows in first sheet
print "content of", sh1.name # name of sheet
for rownum in range(sh1.nrows): # sh1.nrows -> number of rows (ncols -> num columns) 
    print sh1.row_values(rownum)

# rowx and colx (x for Excel) start at 1!
print "row3 col 2:", sh1.cell(rowx=3,colx=2).value

col = sh1.col_values(0)  # column 0 as a list of string or numbers
print '"A" column content:' # python index 0, 1.colunm, called A 
for cell in col: print cell
print sh1.col_values(1) # 2. column, note mix of string (header) and numbers!

对于这个例子,XLS 是:

工作表 1:列表工作

name            latitude longitude   status  color   date
Mount Hood      45.3736  121.6925    active  red     01-ene-01
Mount Jefferson 44.6744  121.7978   dormant yellow  23-sep-05
Three-Fingered  44.478   121.8442   extinct green   
Mount Washington 4.3325  121.8372   extinct green   
South Sister    44.1036  121.7681   active  red 
Diamond Peak    43.5206  122.1486   extinct green   
Mount Thielsen  43.1531  122.0658   extinct green   
Mount Scott     42.923   122.0163   dormant yellow  
Mount McLoughlin 2.445   122.3142   dormant yellow  

表 2:颜色

status  color
active  red
dormant yellow
extinct green

well, as you say:

# reading from a xls file (no .xlsx files, no writing!)
import xlrd  # install xlrd from  http://pypi.python.org/pypi/xlrd

wb = xlrd.open_workbook("YOUR_FILE.xls")  # xls file to read from
sh1 = wb.sheet_by_index(0) # first sheet in workbook
sh2 = wb.sheet_by_name('colors') # sheet called colors

# print all rows in first sheet
print "content of", sh1.name # name of sheet
for rownum in range(sh1.nrows): # sh1.nrows -> number of rows (ncols -> num columns) 
    print sh1.row_values(rownum)

# rowx and colx (x for Excel) start at 1!
print "row3 col 2:", sh1.cell(rowx=3,colx=2).value

col = sh1.col_values(0)  # column 0 as a list of string or numbers
print '"A" column content:' # python index 0, 1.colunm, called A 
for cell in col: print cell
print sh1.col_values(1) # 2. column, note mix of string (header) and numbers!

FOR THIS EXAMPLE THE XLS is:

sheet 1:listing

name            latitude longitude   status  color   date
Mount Hood      45.3736  121.6925    active  red     01-ene-01
Mount Jefferson 44.6744  121.7978   dormant yellow  23-sep-05
Three-Fingered  44.478   121.8442   extinct green   
Mount Washington 4.3325  121.8372   extinct green   
South Sister    44.1036  121.7681   active  red 
Diamond Peak    43.5206  122.1486   extinct green   
Mount Thielsen  43.1531  122.0658   extinct green   
Mount Scott     42.923   122.0163   dormant yellow  
Mount McLoughlin 2.445   122.3142   dormant yellow  

sheet 2:colors

status  color
active  red
dormant yellow
extinct green
苦行僧 2024-11-09 09:52:09

Excel 在内部和 .xls 文件中将日期存储为数字,然后在显示时相应地设置它们的格式。因此,如果您使用 xlrd 天真地读取它们,您将得到数字或字符串。您应该做的是检查单元格的类型是什么,然后自己转换数字。使用 xlrd 的内置函数(例如 xldate_as_tuple())或您自己的函数。

请参阅此问题了解一些信息更多细节。

Excel stores dates as numbers both internally and in .xls files and then formats them accordingly when displaying. Thus, if you read them naively with xlrd, you will get either numbers or strings. What you should do is check what the type of a cell is and then convert the number yourself. Either using xlrd's built-in functions, such as xldate_as_tuple(), or your own function.

Refer to this question for some more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文