2017-02-14 77 views
0

我正在接近這個項目的結束,我再次被卡住了。到目前爲止,程序從網址提取的具體數據,我已經得到它分離的數據和元數據知道我需要做的事情,但datatime.datatime.strptime提取日期

我不知道什麼或如何,數據我所居住這裏是1500線是我與說明書給出了幾個

USGS 14211720 2017-01-30 19:15 PST 28600 P 6.26 P 
USGS 14211720 2017-01-30 19:30 PST 30400 P 6.17 P 
USGS 14211720 2017-01-30 19:45 PST 38300 P 6.03 P 
source site #  date    discharge  stage level 

部分碼是

import datetime 

# define empty lists, we will append the data to them 
dateList = [] 
dischargeList = [] 
stagelevelList = [] 

for line in dataLines: 
    # use 'datatime.datatime.strptime()' to extract date 
    #use the '.strip()' and float() functions to convert values 
    print (dateList)  
    print("we have {} items in the date list".format(len(dateList))) 
    print("we have {} items in the discharge list".format(len(dischargeList))) 
    print("we have {} items in the velocity list".format(len(stagelevelList))) 

回答

0

我從代碼假設評論說,你預計轉換日期從一個字符串到一個datetime對象用datetime.datetime.strptime完成。如果你看看the documentation,你會發現這個轉換使用了一組特殊的格式代碼來指定字符串是如何構造的。爲了您的字符串你會使用

import datetime 

date_str = "2017-01-30 19:15" 
date_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M") 

從DateTime對象,你可以將日期轉換成與你想要的任何格式的字符串。

print(date_obj.strftime("%A %B %d at %H:%M in %Y")) 
# Monday January 30 at 19:15 in 2017 
+0

所以我加入了date_obj線,並增加了一個dateList .append(線)和我沒有定義DATE_STR和dateList是空的,我需要補定義文件的所有三個或什麼都自己被從1400行列表中的數據調用,但我只需要在每個列中放置特定的列 –