[英]Reading .dat file date string
In Python: 在Python中:
Got a .dat file one column is a datestr 'yyyy-mm-dd'. 有一个.dat文件,一列是datestr'yyyy-mm-dd'。 Column years range from 2000, to 2010 I only want to use 2005. 专栏年份从2000年到2010年我只想使用2005年。
How can I successfully read using np.loadtxt, keeping in the same format. 如何使用np.loadtxt成功读取,保持相同的格式。
I am then going to use: 我接下来要用:
time_string = yyyy-mm-dd
doy = int (time.strftime ("%j", time.strptime ( time_string, "%Y, %m, %d")))
to convert yyyy-mm-dd to day of year (1-365) 把yyyy-mm-dd转换成一年中的某一天(1-365)
The question doesn't point any reason to the use of loadtxt
from numpy
, so you actually don't care about how your data is loaded. 问题并没有指出任何理由使用numpy
的loadtxt
,所以你实际上并不关心如何加载数据。 Said that, in this case you simply use dtype=object
for loading it. 说,在这种情况下,你只需使用dtype=object
来加载它。
Suppose this is your .dat file, let us call it d1.dat: 假设这是你的.dat文件,我们称之为d1.dat:
1 2000-01-01 blah
2 2005-01-01 bleh
3 2006-02-03 blih
4 2008-03-04 bloh
5 2010-04-05 bluh
6 2005-03-12 blahr
Then (for example) to load it using numpy
: 然后(例如)使用numpy
加载它:
import numpy
data = numpy.loadtxt('d1.dat', usecols=[1,2], dtype=object)
Now you can apply your function to extract the day of the year from the first column in data: 现在,您可以应用函数从数据的第一列中提取一年中的某一天:
for date, _ in data:
print time.strftime("%j", time.strptime(date, "%Y-%m-%d"))
from datetime import datetime
time_string = '2012-12-31'
dt = datetime.strptime(time_string, '%Y-%m-%d')
print dt.timetuple().tm_yday
366
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.