How to group array of the same name using Python? -
i have on thousand array categories in text file, example:
category a1 , cateogry a2: (array in matlab code)
a1={[2,1,2]}; a1={[4,2,1,2,3]}; a2={[3,3,2,1]}; a2={[4,4,2,2]}; a2={[2,2,1,1,1]};
i use python me read file , group them into:
a1=[{[2,1,2]} {[4,2,1,2,3]}]; a2=[{[3,3,2,1]} {[4,4,2,2]} {[2,2,1,1,1]}];
use dict group, presume mean group strings not valid python containers coming .mat matlab file:
from collections import ordereddict od = ordereddict() open("infile") f: line in f: name, data = line.split("=") od.setdefault(name,[]).append(data.rstrip(";\n")) pprint import pprint pp pp((od.values())) [['{[2,1,2]}', '{[4,2,1,2,3]}'], ['{[3,3,2,1]}', '{[4,4,2,2]}', '{[2,2,1,1,1]}']]
to group data in file write content:
with open("infile", "w") f: k, v in od.items(): f.write("{}=[{}];\n".format(k, " ".join(v))))
output:
a1=[{[2,1,2]} {[4,2,1,2,3]}]; a2=[{[3,3,2,1]} {[4,4,2,2]} {[2,2,1,1,1]}];
which desired output semicolons removed each sub array, elements grouped , semicolon added end of group keep data valid in matlab file.
the collections.ordereddict keep order original file using normal dict have no order.
a safer approach when updating file write temp file replace original file updated using namedtemporaryfile , shutil.move:
from collections import ordereddict od = ordereddict() tempfile import namedtemporaryfile shutil import move open("infile") f, namedtemporaryfile(dir=".", delete=false) temp: line in f: name, data = line.split("=") od.setdefault(name, []).append(data.rstrip("\n;")) k, v in od.items(): temp.write("{}=[{}];\n".format(k, " ".join(v))) move(temp.name, "infile")
if code errored in loop or comp crashed during write, original file preserved.
Comments
Post a Comment