从网页下载的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's wizard also guesses the datatypes better in this exampleJMP 15 的向导还可以更好地猜测此示例中的数据类型
@大卫巴伯
不,你是对的。 我花了两个小时根据我的方法编写了一个脚本,并遇到了许多繁琐的问题。 我现在没有更多时间了。
嗨,lwx228,
下面您将找到一个脚本,它将导入数据、适当地重新编码、将文本放入列、堆栈、拆分以获得最终表。
一些评论,因为这是一种非常奇怪的数据格式:
- 与普通 txt 文件相比,分隔符不一致:
- 逗号用于字段中并作为分隔符,
- 也使用引号,但不是每个元素都使用引号,
- 括号有多种使用方式
- 有时缺失值会出现“null”声明,但有时则不会!
- 第一行没有列名称,它们位于数据字段内。 这使得有必要提取这些并执行堆栈/拆分操作以获取需要分析的列(可能有人有更简单的方法 - 欢迎听到它;) )
- 因此,默认设置将不起作用,因为您将得到每行错误的字段数!
- 最好的方法是读取每行的整个字符串,然后使用重新编码和列实用程序在 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”等)的数据表。 该数据的开头有无关的栅栏“({[”,结尾有“]})”。 替换“{”和“}”,以便获得简单的子列表列表。 然后迭代该列表并一次处理一个子列表。 这变量、值对的顺序很简单,因此您可以一次向数据表添加一行。
我现在没有时间编写一个简单的脚本来演示这种方法。