导入带有日期和时间的txt

时间:2016-01-26 09:23:30

标签: python datetime import

我想导入一个如下所示的txt文件:

time and date   discharge (m3/s)    
1-6-2009 9:50   0    
1-6-2009 9:55   0    
1-6-2009 10:00  0    
1-6-2009 10:05  0    
1-6-2009 10:10  0    
...

日期和时间之间有空格,时间和价值之间有一个标签。如何在python中将日期和时间作为一个值导入?我的脚本看起来像这样:

import scipy    
import matplotlib.dates

kwargs=dict(delimiter =(''),\    
            converters= {0: matplotlib.dates.strpdate2num('%d-%m-%y'),\    
                         1: matplotlib.dates.strpdate2num('%H:%M:%S')},\  
            skip_header= 4,\    
            names=True,\    
            )

catsdata=scipy.genfromtxt('test_tab.txt',**kwargs)    
print catsdata

4 个答案:

答案 0 :(得分:1)

这是一个简单的方法,我只是打印数据

import datetime
f = open('discharge.txt','r')
dis = f.readlines()
f.close()
for i in dis:
  if i.startswith("time"): continue
  dis_date, discharge_rate = i.split('\t')
  discharge_date = datetime.datetime.strptime(str(dis_date),"%d-%m-%Y %H:%M")
  print "Date", discharge_date
  print "discharge rate", discharge_rate

输出:

Date 2009-06-01 09:50:00
discharge rate 0    

Date 2009-06-01 09:55:00
discharge rate 0    

Date 2009-06-01 10:00:00
discharge rate 0    

Date 2009-06-01 10:05:00
discharge rate 0    

Date 2009-06-01 10:10:00
discharge rate 0 

答案 1 :(得分:0)

显然有人打我一个解决方案,应该将其标记为解决方案。但是,这是我的解决方案包含在一个函数中:

from datetime import datetime

def read_data(filename):
    data = []
    with open(filename) as input_file:  
        for line in input_file:
            if not line.startswith('time and date'):
                date_time, discharge = line.split('\t')
                data_item = {'date': datetime.strptime(str(date_time.strip()), "%d-%m-%Y %H:%M"), 'discharge': discharge.strip()}
                data.append(data_item)
    return data

if __name__ == '__main__':
    data = read_data('input.txt')
    for item in data:
        print("Date: {0}, discharge: {1}".format(item['date'], item['discharge']))

该解决方案基本上与萨克森的Rolf解决方案完全相同,但写得有点不同。

答案 2 :(得分:0)

如果你混合使用Python和sed,你可以在阅读文件之前在日期和时间之间添加一些字符:

#function to parse your date and time
def parsetime(v):
    return datetime.datetime.strptime(v, '%d/%m/%Y-%H:%M')

#function to generate a temporary file
def gentpfile():

     temp=os.popen("mktemp")
     temp2=temp.read()
     filename=temp2.strip()
     temp.close()

     return filename

#the function that returns your data    
def readmyfile(myfile):

    tpf=gentpfile()

    os.system("sed \'s/ /-/\' "+ myfile+ " > " + tpf)

    data = np.genfromtxt(tpf,dtype=object,skip_header=4, delimiter='\t', converters = {0: parsetime})

    os.system("rm " + tpf)

    return data

答案 3 :(得分:0)

使用熊猫:

D = pd.read_csv("file.txt", sep="\t")

D["time and date"]=pd.to_datetime(D["time and date"],format="%d-%m-%Y %H:%M")

输出:

        time and date  discharge (m3/s)    
0 2009-06-01 09:50:00                     0
1 2009-06-01 09:55:00                     0
2 2009-06-01 10:00:00                     0
3 2009-06-01 10:05:00                     0
4 2009-06-01 10:10:00                     0