如何解析字符串列表到字典?



我有以下列表:

['name = case1101',
'',
"version = '2';",
'',
'alpha = [',
't1t3t0t0t0t0t1t1.06t0t0t1t1.06t0.94;',
't2t2t21.7t12.7t0t0t1t1.045t-4.98t0t1t1.06t0.94;',
'];',
'',
'beta = [',
't1t232.4t-16.9t10t0t1.06t100t1t332.4t0t0t0t0t0t0t0t0t0t0t0t0;',
't2t40t42.4t50t-40t1.045t100t1t140t0t0t0t0t0t0t0t0t0t0t0t0;',
'];',
'',
'gamma = [',
't1t2t0.01938t0.05917t0.0528t0t0t0t0t0t1t-360t360;',
't1t5t0.05403t0.22304t0.0492t0t0t0t0t0t1t-360t360;',
't2t3t0.04699t0.19797t0.0438t0t0t0t0t0t1t-360t360;',
'];',
'']

我需要将其转换为如下内容:

case = {
"name": "case1101",
"version": "2",
"alpha": np.arrray([[1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.06, 0.0, 0.0, 1.0, 1.06, 0.94]...])
}

。我需要将字符串列表解析为一个字典,其中alpha, beta和gamma以及它们的浮点数数组。

我不知道如何做到这一点,任何建议是感激的!

I have try:

lines = "n".join(lines);
search = re.search("alpha = [([-s0-9e.;]+)]", lines).group(1);
np.array([[float(v) for v in r.strip().split()] for r in s.strip(';nt ').split(';')]);

但想知道是否有更干净的方法来做到这一点。

Code

import re
def to_numpy(v):
' Converts data in capture groups to numpy array'
v = v[2:-1].replace(';', '')                          # Drop brackets [] and ;
rows = v.count('n')                                  # number of rows of data
return np.fromstring(v, dtype = float, sep='t').reshape(rows, -1)   # numpy with 'rows' of data

s = "n".join(lines)             # converts list of strings to string
case = {}
for m in re.finditer(r"(?s)(w+) = (w+|'d+'|[.*?])"):
k, v = m.group(1), m.group(2)
case[k] = to_numpy(v) if v[0] == '[' else v

使用re.finditer docs:

查找字符串中的名称值对
re.finditer(r"(?s)(w+) = (w+|'d+'|[.*?])")
  • (?s):使'。'特殊字符匹配任何字符,包括换行符
  • (w+):用于匹配键。w+匹配Unicode单词字符;这包括任何语言中可以成为单词一部分的大多数字符,以及数字和下划线。
  • = - matches "=";键和值字段之间
  • (w+|'d+'|[.*?]) -匹配单词、引号中的数字或括号中的任何字符

输出

{'name': 'case1101',
'version': "'2'",
'alpha': array([[ 1.   ,  3.   ,  0.   ,  0.   ,  0.   ,  0.   ,  1.   ,  1.06 ,
0.   ,  0.   ,  1.   ,  1.06 ,  0.94 ],
[ 2.   ,  2.   , 21.7  , 12.7  ,  0.   ,  0.   ,  1.   ,  1.045,
-4.98 ,  0.   ,  1.   ,  1.06 ,  0.94 ]]),
'beta': array([[  1.   , 232.4  , -16.9  ,  10.   ,   0.   ,   1.06 , 100.   ,
1.   , 332.4  ,   0.   ,   0.   ,   0.   ,   0.   ,   0.   ,
0.   ,   0.   ,   0.   ,   0.   ,   0.   ,   0.   ,   0.   ],
[  2.   ,  40.   ,  42.4  ,  50.   , -40.   ,   1.045, 100.   ,
1.   , 140.   ,   0.   ,   0.   ,   0.   ,   0.   ,   0.   ,
0.   ,   0.   ,   0.   ,   0.   ,   0.   ,   0.   ,   0.   ]]),
'gamma': array([[ 1.0000e+00,  2.0000e+00,  1.9380e-02,  5.9170e-02,  5.2800e-02,
0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,
1.0000e+00, -3.6000e+02,  3.6000e+02],
[ 1.0000e+00,  5.0000e+00,  5.4030e-02,  2.2304e-01,  4.9200e-02,
0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,
1.0000e+00, -3.6000e+02,  3.6000e+02],
[ 2.0000e+00,  3.0000e+00,  4.6990e-02,  1.9797e-01,  4.3800e-02,
0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00,
1.0000e+00, -3.6000e+02,  3.6000e+02]])}

相关内容

  • 没有找到相关文章

最新更新