使用 Python,我想解析一个格式为 YY-MM-DD 的目录中的每个文件.CSV 但忽略所有其他内容



我是Python的新手,所以我的例子代表了到目前为止所有实验都失败时所聚集的混乱。

#!/usr/bin/python
import glob
import os
import re
path = '/home2/SunnyDataBackup/currentGenerated/SBEAM/'
files = '[1][0-9]-[0-1][0-9]-[0-3][0-9].CSV$' #if I use '*.CSV', it works but doesn't filter the files
fullpath = os.path.join(path, files)
print fullpath
#fullpathC = re.compile(fullpath)
for filename in glob.glob(fullpath):
  print filename

每个文件代表一天的太阳能电池板发电量,但该目录还包含每月摘要CSV,格式为YYYY-MM.CSV。我想分别处理这些文件。我的目标是遍历目录中匹配的文件集,从标题中第 2 行的末尾提取日期,然后是许多行时间 HH:MM 和 Power kWh,小数点后 3 位。我计划将日期与每行的时间连接成时间戳,并将时间戳和功能添加到MariaDB(MySQL)数据库中。解析完每个文件后,我会将其移动到"已处理"子目录,以便在将该程序转换为cron.daily后可以处理创建的任何新文件

CSV 文件15-04-27.CSV如下所示:

sep=;
Version CSV|Tool SunnyBeam11|Linebreaks CR/LF|Delimiter semicolon|Decimalpoint point|Precision 3|Language en-UK|TZO=0|DST|2015.04.27
;SN: serial# removed
;SB model# removed
;serial# removed
Time;Power
HH:mm;kW
00:10;0.000
00:20;0.000
00:30;0.000
00:40;0.000
00:50;0.000
01:00;0.000
01:10;0.000
01:20;0.000
01:30;0.000
01:40;0.000
01:50;0.000
02:00;0.000
02:10;0.000
02:20;0.000
02:30;0.000
02:40;0.000
02:50;0.000
03:00;0.000
03:10;0.000
03:20;0.000
03:30;0.000
03:40;0.000
03:50;0.000
04:00;0.000
04:10;0.000
04:20;0.000
04:30;0.000
04:40;0.000
04:50;0.000
05:00;0.000
05:10;0.000
05:20;0.000
05:30;0.000
05:40;0.000
05:50;0.000
06:00;0.000
06:10;0.024
06:20;0.030
06:30;0.030
06:40;0.042
06:50;0.042
07:00;0.048
07:10;0.048
07:20;0.054
07:30;0.054
07:40;0.060
07:50;0.060
08:00;0.060
08:10;0.066
08:20;0.066
08:30;0.048
08:40;0.870
08:50;1.146
09:00;1.146
09:10;1.116
09:20;0.720
09:30;0.732
09:40;1.536
09:50;1.092
10:00;1.602
10:10;0.870
10:20;1.158
10:30;1.158
10:40;0.492
10:50;1.062
11:00;0.642
11:10;1.302
11:20;1.020
11:30;1.686
11:40;1.458
11:50;1.608
12:00;1.560
12:10;0.954
12:20;1.872
12:30;0.474
12:40;1.350
12:50;1.878
13:00;1.668
13:10;1.116
13:20;0.564
13:30;0.336
13:40;0.282
13:50;0.366
14:00;0.318
14:10;0.294
14:20;1.026
14:30;0.330
14:40;0.672
14:50;1.284
15:00;0.648
15:10;0.894
15:20;0.786
15:30;0.252
15:40;0.252
15:50;0.654
16:00;0.408
16:10;0.408
16:20;0.438
16:30;0.354
16:40;0.288
16:50;0.264
17:00;0.246
17:10;0.228
17:20;0.216
17:30;0.192
17:40;0.156
17:50;0.126
18:00;0.096
18:10;0.078
18:20;0.054
18:30;0.036
18:40;0.030
18:50;0.018
19:00;0.006
19:10;0.000
19:20;0.000
19:30;0.000
19:40;0.000
19:50;0.000
20:00;0.000
20:10;0.000
20:20;0.000
20:30;0.000
20:40;0.000
20:50;0.000
21:00;0.000
21:10;0.000
21:20;0.000
21:30;0.000
21:40;0.000
21:50;0.000
22:00;0.000
22:10;0.000
22:20;0.000
22:30;0.000
22:40;0.000
22:50;0.000
23:00;0.000
23:10;0.000
23:20;0.000
23:30;0.000
23:40;0.000
23:50;0.000
00:00;0.000

E-Today kWh;7.770
E-Total kWh;5534.780

只是为了弄清楚我的问题是什么:仅遍历我正在寻找的文件(格式YY-MM-DD.CSV的文件)的最佳方法是什么?

谢谢

格雷格

这是一个函数,用于返回文件名是否与您的 YY-MM-DD.csv 模式匹配。

from datetime import datetime
def is_dated_csv(filename):
    """
    Return True if filename matches format YY-MM-DD.csv, otherwise False.
    """
    date_format = '%y-%m-%d.csv'
    try:
        datetime.strptime(filename, date_format)
        return True
    except ValueError:
        # filename did not match pattern
        pass
    return False
# Tests
print is_dated_csv('15-01-01.CSV') # True
print is_dated_csv('99-12-31.csv') # True
print is_dated_csv('15-13-32.csv') # False
print is_dated_csv('15-02-30.csv') # False

为测试输出正确的预期结果。请注意,未显示有效日期的文件名(例如上面的 2 月 30 日测试)正确不匹配。使用正则表达式会更难做到这一点。另请注意,.csv扩展不区分大小写。

该函数利用 datetime.datetime.strptime 方法,该方法用于将日期的字符串表示形式转换为datetime对象。在这种情况下,验证转换是否可行很有用,这意味着传递的字符串是指定格式的有效日期字符串。

要使用该函数,您可以执行以下操作:

from os import listdir
path = 'path/to/your/csv/files/'
for filename in listdir(path):
    if is_dated_csv(filename):
        # Do something with the desired .csv file
        pass

编辑:is_dated_csv可以重构以接受其他格式,这将解决您如何检测其他日期模式的问题。有关详细信息,请参阅文档此部分中的格式图表。

你可以将 re 和 os.listdir 组合在一起:

import  re
import  os
r = re.compile(r"^d{2}-d{2}-d{2}.csv$")
print([ f for f in os.listdir(".") if r.search(f)])

如果真的.CSV改成.CSV,如果可以re.I使用:

 re.compile(r"^d{2}-d{2}-d{2}.csv$", re.I)`

对于其他模式:

r2 = re.compile(r"^d{4}-d{2}.csv$")
print([ f for f in os.listdir(".") if r2.search(f)])

最新更新