我正在创建一组正则表达式作为请求以显示来自特定文件的数据。之后,我需要以不同的数字绘制不同的请求:
a= 'regular_expression_1'
Regular_Expression_List.append(a)
b= 'regular_expression_2'
Regular_Expression_List.append(b)
c= 'regular_expression_3'
Regular_Expression_List.append(c)
d= 'regular_expression_4'
Regular_Expression_List.append(d)
我在函数中使用列表
def plot_Results(Log_File):
for reg_expression in enumerate(Regular_Expression_List):
print (signal_sync)
dict_node_info = loadInfoResultsFromRegularExpression(Log_File,reg_expression)
print(dict_node_info)
f = plt.figure(1)
legend = []
for mac, dico_data in dict_node_info.items():
legend.append(mac)
plt.plot(dico_data['timestamp'], dico_data['Counter'])
plt.xlabel('Time (s)')
plt.ylabel('rgular_expression')
plt.title('Results of regular Expression')
legend = plt.legend(legend, loc='upper left', numpoints=1, fontsize=10)
for legend_handle in legend.legendHandles:
legend_handle._legmarker.set_markersize(9)
plt.grid(True)
plt.show()
f.savefig("C:\\Users\\User\\Desktop\\Results_resgular_Expression.png", bbox_inches='tight')
如何基于这些正则表达式创建字典?
my_dict = {}
my_dict = {'a':'regular_expression_1', 'b':'regular_expression_2', 'c':'regular_expression_3','d':'regular_expression_4'}
实际上,我需要该值以便发出请求并循环键,以便基于正则表达式键(例如a,b,c,d)重命名我的图
答案 0 :(得分:2)
如果您想要的只是一个正则表达式的有序字典,则可以使用OrderedDict存储您的re。 (从python 3.7开始,由于保留了订单,因此您无需使用OrderedDict。) 您可以通过以下方式创建正则表达式字典:
import re
from collections import OrderedDict
re_dict = OrderedDict({
'first' : r'\d+', # find all numbers
'second': r'\[\d+\]', # find numbers in [x] format
'third' : r'^The', # all words starting with 'The'
})
f = open('test_data.txt')
text = f.read()
for re_key in re_dict.keys():
re_value = re_dict[re_key]
print(re.findall(re_value, text))
或者在Python 3.7中,您可以简单地使用:
re_dict = {
'first' : r'\d+', # find all numbers
'second': r'\[\d+\]', # find numbers in [x] format
'third' : r'^The', # all words starting with 'The'
}