Input file format is like below:
Label A
{
Zebra,
Lion,
Python,
Cat,
Dog,
Blah,
}
Label B
{
India,
Nigeria,
Malaysia,
Peru,
}
我想为这些元素分配一个默认值,我想我会形成一个多暗的列表/数组然后分配很容易。现在我在变量中有名称和元素。
Code:
from numpy import *
for match in re.finditer(r'Label (\w+)\s+([^\n]+)\s*\{(.*?)\}', input_file_read, re.DOTALL):
Group_name = match.group(1)
Group_elements = str(match.group(3).strip())
ele_names = Group_pins.split(',')
ele_list = [ele.strip() for ele in ele_names]
ele_array.extend(pins_list)
print ele_array
To the above code I also tried:
ele_matrix = np.array(ele_array)
Output:
ele_array = ['Zebra','Lion','Python','Cat','Dog','Blah','India','Nigeria','Malaysia','Peru']
我想:
([A, Zebra, Default_value]
[A, Lion, Default_value]
[A, Python, Default_value]
[A, Cat, Default_value]
[A, Dog, Default_value]
[A, Blah, Default_value]
[B, India, Default_value]
[B, Nigeria, Default_value]
[B, Malaysia, Default_value]
[B, Peru, Default_value])
应该是单矩阵, n * 3 ,其中 n 是总元素小组。
答案 0 :(得分:0)
这应该这样做
import re
# Reading File Data
handle = open("input.txt", "r")
input_file_read = "".join(handle.readlines())
handle.close()
# print input_file_read
_PATTERN_ = re.compile('^[\s]*Label[\s]*(\w)+[\s\n\{]*([^\}]+)', re.MULTILINE);
_DEFAULT_VALUE = 1
_Myarray = []
for match in _PATTERN_.finditer(input_file_read):
Group_name = match.group(1).strip()
# print Group_name
elements = match.group(2).replace(',', '').split()
# print elements
# _Myarray.append([Group_name, i, _DEFAULT_VALUE] for i in elements)
for i in elements:
_Myarray.append([Group_name, i, _DEFAULT_VALUE])
print _Myarray