I have this .txt
file which I need to convert to JSON. There are a lot of three dimensional lookup tables. I can change equal sign to colons, add quotes to the names of parameters etc.
What I need is to remove those %Z_AXIS(...
and change it to square brackets, but I need to distinguish if the %Z_AXIS
is the first one after parameters (then I need to add double square brackets [[
), if the %Z_AXIS
is the last one (then I need to add double square brackets ]]
) or if it is somewhere in between (then I need to add ],[
)
p3_foo_Y_unit = [1, 2, 3, 4, 5]; %arbitrary size
p3_foo_X_unit = [1, 2, 3, 4, 5, 6, 7]; %arbitrary size
p3_foo_unit = [ % p3_foo_X_unit number of columns, p3_foo_Y_unit: number of rows, number of Z_AXIS tag arbitrary, data arbitrary
%Z_AXIS(0.3)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
%Z_AXIS(0.5)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
%Z_AXIS(0.7)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
%Z_AXIS(0.9)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
%Z_AXIS(1.1)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
%Z_AXIS(1.3)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
%Z_AXIS(1.5)
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;];
So it would look somehow like this:
p3_foo_Y_unit = [1, 2, 3, 4, 5];
p3_foo_X_unit = [1, 2, 3, 4, 5, 6, 7];
p3_foo_unit = [[
data
],[
data
],[
data
],[
data
],[
data
],[
data
],[
data]],
I am using this (pseudo code)
with open(txt_file, "r") as f_in:
m_string = f_in.read()
m_string = re.sub(r'%[a-zA-Z]_AXIS[a-zA-Z0-9():@ _.]+', "],[", str(m_string))
The following piece of code does the exact you want
import re
with open("file.txt", "r") as f:
content = f.read()
# Remove all like '%arbitrary size'
content = re.sub(r"];.*?$", "];", content, flags=re.MULTILINE)
# Set open braces
content = re.sub(r"=\s*\[\s*%.*?%\w_AXIS\([\d.]+\)", "= [[", content, flags=re.DOTALL)
# Set close/open braces
content = re.sub(r"%\w_AXIS\([\d.]+\)", "], [", content, flags=re.DOTALL)
# Set close braces
content = re.sub(r"];$", "]]", content, flags=re.DOTALL)
After execution, the content
will contain the following value
p3_foo_Y_unit = [1, 2, 3, 4, 5];
p3_foo_X_unit = [1, 2, 3, 4, 5, 6, 7];
p3_foo_unit = [[
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
], [
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
], [
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
], [
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
], [
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
], [
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
], [
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;]]