简体   繁体   English

Python从Excel文件中读取日期字符串

[英]Python read datestring from excel file

I have data in excel file with Column A as DateTime String and Column B with data. 我在excel文件中有数据,列A为DateTime字符串,列B为数据。 I want to read the excel file in Python and create dictionary of two elements. 我想在Python中阅读excel文件并创建两个元素的字典。 The first being a datetime vector and second being data. 第一个是日期时间向量,第二个是数据。

The data in excel file looks as: Excel文件中的数据如下所示:

DateTime    Marks
1/1/10 23:00     38.86 
2/1/10 12:00     36.19 
1/1/10 13:00     35.10 
1/1/10 14:00     39.87 
5/1/10 15:00     39.48 
1/1/10 16:00     38.64 
1/1/10 17:00     39.19 

I wrote the following snippet to start: 我写了以下代码片段开始:

import xlrd
import os.path
wb = xlrd.open_workbook(os.path.join('C:\Users\JD\Documents\RandomNumber','DateTimeData.xlsx'))

But I have no clue what to do next? 但是我不知道下一步该怎么做?

Here is a hint anyway. 无论如何,这是一个提示。 Use a for loop to parse through the file. 使用for循环来解析文件。

datetime=[]
data=[]
for line in wb:
      datetime=line.split()[0:2]
      data=line.split().pop()
      strdate=' '.join(datetime)
      if 'DateTime' not in strdate:
        print strdate
        print data

This prints out date/time and the data. 打印出日期/时间和数据。 Hope this helps. 希望这可以帮助。

import os
import xlrd
import datetime


excel = os.path.join(os.getcwd(), 'test.xlsx')
book = xlrd.open_workbook(excel, 'r')
data = book.sheet_by_name('Sheet1')

for row in range(data.nrows)[1:]:
    excel_date = data.row_values(rowx=row)[0]
    print datetime.datetime(* xlrd.xldate_as_tuple(excel_date, datemode=0))

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

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