Python to read a column of dates from Excel
As shown below:
import xlrd import datetime file=u"Vodka.xls."# Note that reading Chinese filenames is slightly handled data=xlrd.open_workbook(file) table = data.sheet_by_index(0)# Read Excel files by index colContent=table.col_values(1)#Read a column, the date is in the second column # nrows= # of lines print nrows ncols = # of columns print "With %s columns"%ncols # Just want to test, randomly output or not # Read date format from Excel, need to be converted to date format in Python, there are two methods of conversion print xlrd.xldate_as_tuple((2,1).value, 0) # The first is converted to a tuple form (2,1).value is the value of a date cell, the test print .xldate_as_datetime((1,1).value, 0) # This is the second direct conversion to a datetime object # Loop reading xlist=[] for i in range(1,nrows): x=xlrd.xldate_as_tuple((i,1).value,0) #convert to tuple form xldate_as_tuple # The second parameter has two values, 0 or 1. 0 is the date based on 1900-01-01, while 1 is the date based on 1904-01-01. The function returns a tuple whose values are similar: (year, month, day, hour, minute, nearest_second) (x[0:3])#I just want the first one. I don't want the hours, minutes and seconds. for i in xlist: print i# Now get what you need # This is the second method of loop fetching ylist=[] for i in range(1,nrows): y= .xldate_as_datetime((i,1).value,0) (y) for i in ylist: print i The direct function for #Python to do time differences is datetime d1 = (2015,10,7) d2 = (2013,8,15) print type(d1) print((d1-d2).days) However, the date format read with the above method is not suitable for calling this function directly,Because the type is different。(ExcelThere are functions in there to calculate the date difference,Can be used directly)。。Continue to add。。
Above this Python from Excel to read the date of a column is all I have to share with you, I hope to give you a reference, and I hope you support me more.
Related articles
Python Metaclass Examples Explained
This article introduces the Python metaclass example analysis, the main task of this article is to give you a thorough understanding of what is metaclasses, the need for friends can refer to the following2018-04-04Python Web-based implementation of m3u8 video playback example
This article introduces the python Web-based implementation of m3u8 video playback examples of relevant information, need friends can refer to the following2023-06-06How to record your roommate's jitterbug online time using python
This article mainly introduces how to use python record roommate's jitterbug online time, this article through the example code combined with graphic to give you a very detailed study or work has a certain reference value, need friends can refer to the following2020-06-06python opencv image processing (Sketch, Nostalgia, Light, Years, Filter Principle and Implementation)
This article introduces python based on opencv image processing, to help you better understand and use python, interested parties can understand the following2020-12-12Python Pillow Image Invert
Today I would like to share with you an article on Python Pillow Image Invert, I think the content is quite good, and now share it with you, has a good reference value, the need for friends to follow along with the editor to see it!2019-01-01About Python's Advanced Data Structures and Algorithms
This article introduces the Python advanced data structures and algorithms, mastering these data structures and algorithms will help us to solve a variety of problems in the actual programming, to improve our programming skills and level, the need for friends can refer to the following2023-04-04Introduction to Django Framework Templates
Today I would like to share with you an introduction to the Django framework template, I think the content is quite good, and now share it with you, with good reference value, the need for friends to follow along with the editor to see it!2019-01-01Python secondary effects that are on fire are easy to master
This article introduces the use of python to write the secondary effects of the change in the small program, describes in detail the whole idea and process as well as the code, read through this piece of learning or work has a certain value, the need for friends can refer to the following2021-09-09An article that takes you through the use of pymysql in Python
pymysql is linked to mysql to achieve python window and mysql database interaction library, very easy to operate, this article will explain in detail how to master and use the library, interested you can learn about it!2023-02-02Python pyecharts Map map data does not show the reason and the perfect solution
This article introduces you to the Python pyecharts Map Map data does not show the reasons and solutions, pyecharts is a combination of python and echarts powerful data visualization tools, the text through the graphic and code samples introduced in great detail, you can refer to below2023-12-12