从网页下载的txt文件,这个文件有数据,如何用JSL把这样的txt文件处理成表格数据?
我无法用JSON处理它。谢谢专家!

// the U.txt file has 2 extra characters at the
// start and 3 extra characters at the end.
// visual inspection is the only way I know to
// figure that out.
// using JMP 15's json wizard
x = Load Text File( "E:/U.txt" );
x = Substr( x, 3, Length( x ) - 5 );
Open( Char To Blob( x ), jsonwizard( 0 ) );
// using JMP 14's basic json support
x = Load Text File( "E:/U.txt" );
x = Substr( x, 3, Length( x ) - 5 );
Open( Char To Blob( x ), "json" );
如果您有 JMP 15,您还可以交互式地使用该向导(使用 JsonWizard(1) 查看 GUI。)它仍然需要帮助删除多余的字符。上面的 JSL 将 txt 加载到 x 中,使用 substr() 删除第 2 个和第 3 个字符,然后通过 blob 将数据传递给 open(),而无需重新写入磁盘。 如果重写到磁盘,还可以将扩展名更改为 .json。
JMP 15's wizard also guesses the datatypes better in this exampleJMP 15 的向导在此示例中也能更好地猜测数据类型
@大卫巴布尔
不,你是对的。 我花了两个小时根据我的方法写了一个脚本,遇到了很多繁琐的问题。 我现在没有更多时间了。
嗨lwx228,
您将在下面找到一个脚本,该脚本将导入数据、适当地重新编码、将文本放入列、堆栈、拆分以获得最终表格。
一些评论,因为这是一种非常奇怪的数据格式:
- 与通常的 txt 文件相比,分隔符不一致:
- 逗号在字段中用作分隔符,
- 也使用引号,但不是针对每个元素,
- 括号有多种使用方式
- 有时会有缺失值的“空”语句,但有时不会!
- 第一行没有列名,它们在数据字段中。 这使得有必要提取它们并进行堆栈/拆分操作以获取您需要的列进行分析(可能有人有更简单的方法 - 欢迎听到它;) )
- 因此,默认设置将不起作用,因为您将得到错误的每行字段数!
- 最好是每行读入整个字符串,然后使用重新编码和列实用程序在 JMP 中完成其余部分。 希望这些也可以与类似的文件一起使用。
- 要编写这些内容的脚本,您必须测试每个步骤,如果该步骤似乎没问题,请将脚本保存/复制到脚本窗口并执行下一步。
- 下面的脚本就是这样创建的。
Names Default to Here(1);
//Open your txt file using special settings, like } as end of line, uncheck column names, data starts row 1, ...
dt = Open(
"YOURPATH\U.txt", // exchange YOURPATH with your path to the file
columns( New Column( "c000001", Character, "Nominal" ) ),
Import Settings(
End Of Line( Semicolon, Other( "}" ) ),
End Of Field( CSV( 0 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 0 ),
Column Names Start( 1 ),
Data Starts( 1 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
);
// Now you have one column with in each row all information after "name"
// Remove last two lines as they are just filled with brackets
dt << Clear Selected Row States;
m = matrix({N Rows(dt)-1, N Rows(dt)});
dt << Select Rows( m);
wait(2);
dt << Delete Rows();
// Best to use Recode and Column utility functions to seperate all information
dt << New Column("recoded","Character",Formula(Recode(
:c000001,
{Substitute( _rcNow, ",{", "" ), Substitute( _rcNow, "({[{", "" ),
Substitute( _rcNow, ":", "," ), Substitute( _rcNow, "\!",\!"", ";" ),
Substitute( _rcNow, ";;", ";null;" ),
Substitute( _rcNow, "\!"", "" ), Substitute( _rcNow, ",null,", ";null;" ),
Substitute( _rcNow, ",null", ";null" )
}
)));
// Get all text seperated by ; in seperated columns
dt << Text To Columns( delimiter( ";" ), columns( :recoded ) );
dt << Delete Columns(Column(1),Column(2));
// Now you have to stack the data appropriately
colNames = dt << Get Column Names();
dt_stack = dt << Stack(
Columns(colNames),
Source Label Column( "ColName" ),
Stacked Data Column( "Data" ),
Number of Series( 2 )
);
dt_stack << Delete Columns(1,3);
// Just to Split it again to get the columns with the correct names as well the data respectively
dt_final = dt_stack << Split(
Split By( :Data ),
Split( :Data 2 ),
Sort by Column Property
);
// Close data tables you do not need anymore
close(dt_stack, NoSave);
close(dt, NoSave);
// Finally you get an alphabetic order and all data is in character. So you now need to change this appropriately.
经历这个过程很有趣,但需要一些时间来克服上述奇怪的数据挑战。 但是由于内置的脚本和公式不是太多:)
另一个提示:要获取重新编码公式,请使用重新编码并将其另存为公式列,查看公式编辑器即可。
最好,
马丁
创建一个包含必要数据列(例如,“name”、“cname”等)的数据表。 此数据的末尾有无关的围栏“({[”在开头和“]})”。 替换“{”和“}”,这样您就有了一个简单的子列表列表。 然后迭代列表并一次处理一个子列表。 这变量,值对的顺序很简单,因此您可以一次向数据表添加一行。
我现在没有时间编写一个简单的脚本来演示这种方法。