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

How to convert lookup tables from txt to 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))

我不确定您是否打算用“]]”替换最后一次出现的位置,因为那样会遗漏最后一个数组。它已经在文件末尾有一个“]”,你只是想让它成为“]]”吗?这是 js 代码,它将产生我相信您所要求的内容。您可以更改以指定 input/output 文件。

//  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");

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

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