如何将查找表从 txt 转换为 JSON

发布于 2025-01-10 19:51:35 字数 1817 浏览 0 评论 0原文

我有这个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2025-01-17 19:51:35

下面的代码完全符合您的要求

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)

执行后,content 将包含以下值

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;]]

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"];.*?
quot;, "];", 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"];
quot;, "]]", 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;]]
人生百味 2025-01-17 19:51:35

我不确定您是否打算用“]]”替换最后一个出现的地方,因为这会遗漏最后一个数组。文件末尾已经有一个“]”,您只想将其变为“]]”吗?这是 js 代码,它将产生我相信您所要求的内容。您可以更改以指定输入/输出文件。

//  convert txt file as specified and create new textfile as per required format

const cl = (...args) => console.log(...args);
const fs = require("fs");

// cnvTxt is used to find and replace the "%Z_AXIS(x,y)" (12 char) with specified new text
const cnvTxt = (txt, txtnew) =>
  txt.substring(0, a) + txtnew + txt.substring(a + 12);
const oldTxt = "%Z_AXIS";
const firstNewTxt = "[[";
const secondNewTxt = "],[";
const inputFile = "z.txt"; // copied your txt to z.txt
const outputFile = "zoutput.txt";

let txt1 = fs.readFileSync(inputFile, "utf8");
let txt2 = txt1;

let a = txt1.indexOf(oldTxt); // first occurence position

txt2 = cnvTxt(txt2, firstNewTxt); // first replacement
let isZ = true;
while (isZ) {
  a = txt2.indexOf(oldTxt);
  if (a === -1) {
    isZ = false;
  } else {
    txt2 = cnvTxt(txt2, secondNewTxt);
  }
}

// Write data in 'Output.txt' .
let data = txt2;
fs.writeFile("zoutput.txt", data, (err) => {
  // In case of a error throw err.
  if (err) throw err;
});
cl("Output written to zoutput.txt");

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.

//  convert txt file as specified and create new textfile as per required format

const cl = (...args) => console.log(...args);
const fs = require("fs");

// cnvTxt is used to find and replace the "%Z_AXIS(x,y)" (12 char) with specified new text
const cnvTxt = (txt, txtnew) =>
  txt.substring(0, a) + txtnew + txt.substring(a + 12);
const oldTxt = "%Z_AXIS";
const firstNewTxt = "[[";
const secondNewTxt = "],[";
const inputFile = "z.txt"; // copied your txt to z.txt
const outputFile = "zoutput.txt";

let txt1 = fs.readFileSync(inputFile, "utf8");
let txt2 = txt1;

let a = txt1.indexOf(oldTxt); // first occurence position

txt2 = cnvTxt(txt2, firstNewTxt); // first replacement
let isZ = true;
while (isZ) {
  a = txt2.indexOf(oldTxt);
  if (a === -1) {
    isZ = false;
  } else {
    txt2 = cnvTxt(txt2, secondNewTxt);
  }
}

// Write data in 'Output.txt' .
let data = txt2;
fs.writeFile("zoutput.txt", data, (err) => {
  // In case of a error throw err.
  if (err) throw err;
});
cl("Output written to zoutput.txt");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文