【问题标题】:Python: Regular expression use re.searchPython:正则表达式使用 re.search
【发布时间】:2013-03-24 15:15:29
【问题描述】:

我正在遍历包含许多 csv 文件的目录结构,我只对该目录中的一些 csv 文件感兴趣:

    if os.path.exists(lang_dir):
        dirs = os.listdir(lang_dir)
        for filename in dirs:
            if re.search(r'-.+-template-users-data.csv$',filename):

但由于某种原因无法识别带有zu-en-template-users-data.csv 的文件名,我感觉文件名中的字母u 与此有关。只是为了仔细检查上面的代码段,我直接进入文件夹并使用 python 解释器尝试了它,并且使用 python 解释器文件确实得到了正确识别。

>>> import re
>>> import os
>>> dirs = os.listdir("PATH_FOR_THE_DIR/Data/2013_03_06_20_34/zu")
>>> for item in dirs:
...     if re.search(r'-.+-template-users-data.csv$',item):
...             print item
... 
zu-ab-template-users-data.csv
zu-ace-template-users-data.csv
zu-af-template-users-data.csv
zu-ak-template-users-data.csv
zu-als-template-users-data.csv
...

如您所见,所有以zu 开头的文件都出现了。这意味着我的正则表达式代码段是正确的吗? (据我了解)

这是我的代码:

def templateUserCountStats(root_dir_path, lang_code_file_path):
    #dictionary to hold the template count data structure
    template_count_dict = dict()
    # getting lang codes from csv file
    for lang in getLanguageCodes(lang_code_file_path):
        # root level key of the dictionary
        template_count_dict[lang] = dict()
        lang_dir = os.path.join(root_dir_path, lang)
        # get all the files as s list in lang dir
        if os.path.exists(lang_dir):
            dirs = os.listdir(lang_dir)
            for filename in dirs:
                if re.search(r'-.+-template-users-data.csv$',filename):
                    lang2 = filename.split("-")[1]
                    #path = os.path.join(lang_dir, filename)
                    path = os.path.expanduser(lang_dir + '/' + filename)
                    #with open(path, 'rb') as template_user_data_file:
                    try:
                        template_user_data_file = open(path, 'r')
                        try:
                            csv_file_reader = csv.reader(template_user_data_file)
                            csv_file_reader.next()
                            # initializing user count for each language
                            template_count_dict[lang][lang2] = dict()
                            template_count_dict[lang][lang2]['level1'] = 0
                            template_count_dict[lang][lang2]['level2'] = 0
                            template_count_dict[lang][lang2]['level3'] = 0
                            template_count_dict[lang][lang2]['level4'] = 0
                            template_count_dict[lang][lang2]['level5'] = 0
                            template_count_dict[lang][lang2]['levelN'] = 0
                            #print filename
                            for row in csv_file_reader:
                                if row[0] == '1':
                                    template_count_dict[lang][lang2]['level1'] = template_count_dict[lang][lang2]['level1'] + 1
                                if row[0] == '2':
                                    template_count_dict[lang][lang2]['level2'] = template_count_dict[lang][lang2]['level2'] + 1
                                if row[0] == '3':
                                    template_count_dict[lang][lang2]['level3'] = template_count_dict[lang][lang2]['level3'] + 1
                                if row[0] == '4':
                                    template_count_dict[lang][lang2]['level4'] = template_count_dict[lang][lang2]['level4'] + 1
                                if row[0] == '5':
                                    template_count_dict[lang][lang2]['level5'] = template_count_dict[lang][lang2]['level5'] + 1
                                if row[0] == 'N':
                                    template_count_dict[lang][lang2]['levelN'] = template_count_dict[lang][lang2]['levelN'] + 1
                        except csv.Error, e:
                            print e
                    except Exception, e:
                        print e
                        logging.error(e)
            else:
                print "path doesn't exist"
    return template_count_dict

【问题讨论】:

  • 首先你说zu-en-template-users-data.csv不匹配,然后你说所有以zu开头的文件都匹配。实际发生了什么,你想要发生什么?您确定文件zu-en-template-users-data.csv 甚至首先存在吗? (另外,我向你保证,一个裸露的 u 不会被正则表达式特殊处理。)
  • 你为什么不使用 Python 的 glob 模块?
  • 我刚刚编辑了这个问题。我认为我的正则表达式不正确,所以我用 python 解释器尝试了它

标签: python regex csv file-io


【解决方案1】:

这是因为正则表达式如何将 u 解释为模式...?

不,这不可能是原因。

>>> bool(re.search(r'-.+-template-users-data.csv$', 'zu-en-template-users-data.csv'))
True

您的 re 模式应该可以工作,问题出在其他地方。

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2015-08-02
    • 2020-08-12
    • 2014-07-22
    • 2019-10-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多