Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python,我希望以YY-MM-DD.CSV格式解析一个目录中的每个文件,但忽略所有其他文件_Python_Regex_File_Filter - Fatal编程技术网

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

使用Python,我希望以YY-MM-DD.CSV格式解析一个目录中的每个文件,但忽略所有其他文件,python,regex,file,filter,Python,Regex,File,Filter,我是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 ful

我是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和功率kWh,小数点后3位。我计划将日期与每行的时间连接成一个时间戳,并将时间戳和电源添加到MariaDBMySQL数据库中。解析完每个文件后,我会将其移动到“已处理”子目录中,以便在该程序转换为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格式)的最佳方式是什么

谢谢


Greg

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

对于另一种模式:

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可以重构为接受其他格式,这将解决如何检测其他日期模式的问题。有关更多信息,请参阅

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