[PYTHON]XLRD Reading 2 ways of Excel date type

Source: Internet
Author: User

There is a excle table to do some filtering and then write to the database, but the date type of the cell is taken out is a number, so query the next solution.


The main code structure

data = Xlrd.open_workbook (excel_path) Table = data.sheet_by_index (0) lines = Table.nrowscols = Table.ncolsprint U ' the Total line was%s, cols is%s '% (lines, cols)

To read a cell:

Table.cell (x, y). Value
X: Line

Y: Column

Rows, the columns start at 0


* Time type conversion, convert Excel time to Python time (two ways)

Excel a cell 2014/7/8

Xlrd.xldate_as_tuple (Table.cell (2,2). Value, 0)   #转化为元组形式 (7, 8, 0, 0, 0) xlrd.xldate.xldate_as_datetime ( Table.cell (2,2). Value, 1)   #直接转化为datetime对象datetime. DateTime (2018, 7, 9, 0, 0) Table.cell (2,2). Value   # No conversions 41828.0

Source Code View:

# @param xldate the Excel number# @param datemode 0:1900-based, 1:1904-based.xldate_as_tuple (Xldate, Datemode)  
Entering a cell of a date type returns a tuple that consists of a time structure. Ability to compose time types based on this tuple

Datemode has 2 options basically, we're going to use a 1900-based timestamp.


# # Convert An Excel date/time number into a datetime.datetime object.## @param xldate the Excel number# @param datemode 0 : 1900-based, 1:1904-based.## @return a datetime.datetime () object. #def xldate_as_datetime (xldate, Datemode)
The input parameters are the same as above. However, the return value is a datetime type and does not need to be converted


Of course, both of these functions have corresponding inverse functions. Change the Python type to the corresponding excle time type.

?

This article is derived from "Orangleliu notebook " blog. Be sure to keep this source http://blog.csdn.net/orangleliu/article/details/38476881

[PYTHON]XLRD Reading 2 ways of Excel date type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.