首页 > 解决方案 > 运行 RegEx 代码块时,Spyder 控制台不断重启

问题描述

我正在准备一个代码来识别名称中带有“日期”一词的数据框中的列。我正在使用 RegEx 比较使用 re.split() 函数从原始名称生成的子字符串。这是整个代码:

import pandas as pd
import numpy as np
import re

df = pd.read_excel(r'C:\Users\rishi\Desktop\PGDBDA\Dataset\Dataset for Date Operations.xlsx')
#print(df)
# Dataset is loaded into Pandas dataframe

column_name = [names for names in df.columns]
#print(column_name)
# The column names are extracted into a list called column_name.
# We plan use a mechanism to identify the sub-string 'date' from the elements in column_name.

name_split = []
for index in column_name:
name_split.append(re.split(' |-|_',index))
#print(name_split)
# Using RegEx we are able to split the elements in the column name based on a set of dilimiters.
# We are grouping them in a list of lists nammed as name_split.  

column_index = []
column_count = 0
regex_pattern = re.compile(r"\bdate\b", re.IGNORECASE)

for index in name_split:
    for elements in index:
       if re.search(regex_pattern, elements) != None:
                    column_index.append(column_count)
                    exit()        
    column_count+=1        
print(column_index)
# Will tell us all the columns with 'date' in their names, by stating the index no of the column.

问题是每次我运行这部分代码时:

column_index = []
column_count = 0
regex_pattern = re.compile(r"\bdate\b", re.IGNORECASE)

for index in name_split:
    for elements in index:
        if re.search(regex_pattern, elements) != None:
                   column_index.append(column_count)
                exit()        
column_count+=1        
print(column_index)
# Will tell us all the columns with 'date' in their names, by stating the index no of the column.

控制台不断崩溃和重新加载。对此问题的任何见解都将受到高度赞赏。

标签: pythonregexanacondaspyder

解决方案


推荐阅读