如何将查找表从 txt 转换为 JSON
我有这个 .txt
文件,需要将其转换为 JSON。有很多三维查找表。我可以将等号更改为冒号,在参数名称中添加引号等。
我需要的是删除那些 %Z_AXIS(...
并将其更改为方括号,但我需要区分是否%Z_AXIS
是参数后面的第一个(然后我需要添加双方括号[[
),如果 %Z_AXIS
是最后一个一个(然后我需要添加双平方括号 ]]
)或者如果它位于两者之间(那么我需要添加 ],[
)
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;];
所以它看起来像这样:
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]],
我正在使用这个(伪代码)
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))
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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的代码完全符合您的要求
执行后,
content
将包含以下值The following piece of code does the exact you want
After execution, the
content
will contain the following value我不确定您是否打算用“]]”替换最后一个出现的地方,因为这会遗漏最后一个数组。文件末尾已经有一个“]”,您只想将其变为“]]”吗?这是 js 代码,它将产生我相信您所要求的内容。您可以更改以指定输入/输出文件。
I am not sure that you meant to replace last occurrence with "]]" as that would leave out the last array. It has a "]" at end of file already, do you just want that to be "]]" ? Here is js code that will produce what I believe you have asked for. You can change to specify the input/output file.