繁体   English   中英

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

[英]Using Python, I want to parse each file in one directory with format YY-MM-DD.CSV but ignore all others

我是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

每个文件代表一天的太阳能电池板生成量,但该目录还包含YYYY-MM.CSV格式的每月摘要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格式的文件)的最佳方法是什么?

谢谢,

格雷格

您可以将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)])

这是一个返回文件名是否与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可以重构为接受其他格式,这将解决您如何也检测其他日期格式的问题。 有关更多信息,请参阅文档本节中的格式表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM