python - read somewhat structured text file into array in C++ -
i reproduce following python code in c++, have run trouble. function read_file reads text file, tests first word in each line see if integer. if first word integer (4 or more digits) of words on line added list, z, floats. in other case line added string list. list of lists (z) converted 2d numpy array , returned rest.
def read_file(f): srchp = re.compile(r'^\d{4,}\s') # beg. of line, digit min 4, white space f = open(f) rest = [] z = [x.strip() x in f.readlines()] # read file, strip whitespace @ beg./end of line, #store in z list of strings. each line @ own offset in range(len(z)-1,-1,-1): if not srchp.search(z[i]): #if regex not match rest.append(z.pop(i)) #append list rest else: z[i] = map(float,z[i].split()) f.close() return numpy.array(z),rest
what data types should use containers in c++ (vector of vectors? arrays?)? @ end of day want use array statistical analysis. i'd grateful in converting code c++.
the following excerpt file needs read.
temp_inf 700.000000 scalar name value type dimensions temp_ref 25.0000000 scalar ***** post1 element table listing ***** stat mixed mixed mixed mixed elem x y z temp 23261 0.56292e-03 -0.96401e-02 0.24093 755.91 23262 -0.16635e-03 -0.97998e-02 0.24080 756.25 23263 -0.17039e-03 -0.10374e-01 0.24025 757.65 23264 0.12895e-02 -0.74483e-02 0.24242 751.64 23265 0.67515e-03 -0.80538e-02 0.24209 752.62 23266 0.10350e-02 -0.86614e-02 0.24164 753.92 23267 0.56032e-03 -0.88420e-02 0.24105 756.49 23268 0.13782e-02 -0.10792e-01 0.23978 758.74
because each row looks holds int , float, decent idea declare struct
information.
struct row { int elem; float x, y, z, temp; };
now can can create vector<row>
hold information.
vector<row> rows;
for each row, can insert elements so:
row r; cin >> r.elem >> r.x >> r.y >> r.z >> r.temp; rows.push_back(r);
Comments
Post a Comment