python - 使用 pandas python 从文件中读取特定日期行

标签 python file datetime pandas

我正在尝试读入许多文件。每个文件是每日数据文件,每 10 分钟有一次数据。每个文件中的数据都像这样“分块”:

2015-11-08 00:10:00 00:10:00
#    z  speed    dir      W   sigW       bck   error 
30   3.32  111.9   0.15   0.12  1.50E+05       0
40   3.85  108.2   0.07   0.14  7.75E+04       0
50   4.20  107.9   0.06   0.15  4.73E+04       0
60   4.16  108.5   0.03   0.19  2.73E+04       0
70   4.06   93.6   0.03   0.23  9.07E+04       0
80   4.06   93.8   0.07   0.28  1.36E+05       0

2015-11-08 00:20:00 00:10:00
#    z  speed    dir      W   sigW       bck   error 
30   3.79  120.9   0.15   0.11  7.79E+05       0
40   4.36  115.6   0.04   0.13  2.42E+05       0
50   4.71  113.6   0.07   0.14  6.84E+04       0
60   5.00  113.3   0.13   0.17  1.16E+04       0
70   4.29   94.2   0.22   0.20  1.38E+05       0
80   4.54   94.1   0.11   0.25  1.76E+05       0

2015-11-08 00:30:00 00:10:00
#    z  speed    dir      W   sigW       bck   error 
30   3.86  113.6   0.13   0.10  2.68E+05       0
40   4.34  116.1   0.09   0.11  1.41E+05       0
50   5.02  112.8   0.04   0.12  7.28E+04       0
60   5.36  110.5   0.01   0.14  5.81E+04       0
70   4.67   95.4   0.14   0.16  7.69E+04       0
80   4.56   95.0   0.15   0.21  9.84E+04       0

...

全天,该文件每 10 分钟就这样继续播放一次。该文件的文件名是151108.mnd。我希望我的代码读取 11 月的所有文件,所以 1511??.mnd 我希望我的代码读取整个月的每天文件,抓取所有日期时间行,因此对于我刚刚展示的部分数据文件示例我希望我的代码能够获取 2015-11-08 00:10:00、2015-11-08 00:20:00、2015-11-08 00:30:00 等存储为变量,然后转到第二天文件(151109.mnd)并获取所有日期时间行并存储为日期变量并附加到之前存储的日期。以此类推整个月。这是我到目前为止的代码:

import pandas as pd
import glob
import datetime

filename = glob.glob('1511??.mnd')
data_nov15_hereford = pd.DataFrame()
frames = []
dates = []
counter = 1
for i in filename:
    f_nov15_hereford = pd.read_csv(i, skiprows = 32)
    for line in f_nov15_hereford:
        if line.startswith("20"):
            print line
            date_object = datetime.datetime.strptime(line[:-6], '%Y-%m-%d %H:%M:%S %f')
            dates.append(date_object)
            counter = 0
        else:
            counter += 1 
    frames.append(f_nov15_hereford) 
data_nov15_hereford = pd.concat(frames,ignore_index=True)
data_nov15_hereford = data_nov15_hereford.convert_objects(convert_numeric=True)


print dates

这段代码有一些问题,因为当我打印日期时,它会打印出每个日期的两份副本,并且它也只打印出每个文件的第一个日期,所以 2015-11-08 00:10:00, 2015-11-09 00:10:00 等。它不会在每个文件中逐行存储,然后一旦存储该文件中的所有日期,就会像我想要的那样移动到下一个文件。相反,它只是获取每个文件中的第一个日期。对这段代码有帮助吗?有没有更简单的方法来做我想做的事?谢谢!

最佳答案

一些观察:

首先:为什么您只获取文件中的第一个日期:

f_nov15_hereford = pd.read_csv(i, skiprows = 32)
for line in f_nov15_hereford:
    if line.startswith("20"):

第一行将文件读取到 pandas 数据帧中。第二行迭代数据帧的列,而不是行。结果,最后一行检查该列是否以“20”开头。每个文件只会发生一次。

第二:counter被初始化并且它的值被改变,但它从未被使用。我认为它的目的是用来跳过文件中的行。

第三:将所有日期收集到 Python 列表中,然后根据需要将其转换为 pandas 数据框可能会更简单。

import pandas as pd
import glob
import datetime as dt

# number of lines to skip before the first date
offset = 32

# number of lines from one date to the next
recordlength = 9

pattern = '1511??.mnd'

dates = []

for filename in glob.iglob(pattern):

    with open(filename) as datafile:

        count = -offset
        for line in datafile:
            if count == 0:
                fmt = '%Y-%m-%d %H:%M:%S %f'
                date_object = dt.datetime.strptime(line[:-6], fmt)
                dates.append(date_object)

            count += 1 

            if count == recordlength:
                count = 0

data_nov15_hereford = pd.DataFrame(dates, columns=['Dates'])

print dates

关于python - 使用 pandas python 从文件中读取特定日期行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731057/

相关文章:

python - while 语句中的 try block

java - Java中的平台无关路径

c# - Windows C# 中找不到文件异常

c# - 为什么 "DateTime.Now.ToString("hh tt", new CultureInfo ("de"))"在不同版本的 .Net 中返回不同的结果?

java - 导入 ARFF 文件期间无法解析的日期 'yyyy-MM-dd HH:mm:ss'

python - 为什么 Tkinter 的 askdirectory() 在 Windows 上返回正斜杠?

python - 为什么 jpeg 不能从 django 正确保存到 AWS S3

python - 有没有为 AVX 指令编译的 TensorFlow 版本?

php - 通过增量保护重复文件名

ruby - 在 Ruby 中解析 "X years and Y weeks ago"相似的字符串