简体   繁体   English

Python xlrd读为字符串

[英]Python xlrd read as string

I'm having difficulties in reading a particular cell value from Excel in xlrd. 我在从xlrd中读取Excel中的特定单元格值时遇到了困难。 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? 我知道有解决方案将其转换为python日期格式,但是我可以直接读取xlrd中的字符串值吗?

xlrd does NOT convert dates to float. xlrd不会将日期转换为浮动。 Excel stores dates as floats. Excel将日期存储为浮点数。

Quoting from the xlrd documentation (scroll down a page): xlrd文档引用(向下滚动页面):

Dates in Excel spreadsheets 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: Excel日期有几个问题:

(1) Dates are not stored as a separate data type; (1)日期不作为单独的数据类型存储; 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. 它们存储为浮点数,你必须依赖(a)在Excel中应用它们的“数字格式”和/或(b)知道哪些单元格应该在其中包含日期。 This module helps with (a) by inspecting the format that has been applied to each number cell; 该模块通过检查已应用于每个数字单元格的格式来帮助(a); 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. (2)...当使用此包的xldate_as_tuple()函数转换工作簿中的数字时,必须使用Book对象的datemode属性。

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). 另请参阅Cell类的部分以了解单元格的类型,以及提取单元格类型的各种Sheet方法 (文本,数字,日期,布尔值等)。

Check out python-excel.org for info on other Python Excel packages. 查看python-excel.org了解其他Python Excel包的信息。

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: 对于这个例子,XLS是:

sheet 1:listing 表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  

sheet 2:colors 表2:颜色

status  color
active  red
dormant yellow
extinct green

Excel stores dates as numbers both internally and in .xls files and then formats them accordingly when displaying. Excel将日期作为内部和.xls文件中的数字存储,然后在显示时相应地格式化它们。 Thus, if you read them naively with xlrd , you will get either numbers or strings. 因此,如果您使用xlrd天真地阅读它们,您将获得数字或字符串。 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. 使用xlrd的内置函数,例如xldate_as_tuple() ,或者你自己的函数。

Refer to this question for some more details. 有关更多详细信息,请参阅此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM