【问题标题】:Maple: How to parse such CSV (Comma Separated Values) document?Maple:如何解析这样的 CSV(逗号分隔值)文档?
【发布时间】:2011-02-01 08:15:59
【问题描述】:

所以我有一个包含这些内容的大 txt 文件(比如 20mbs 长)

20 30 40 550 60 70 80 91
20 30 40 50 60 70 80 92
20 30 40 50 60 70 80 93
20 30 40 50 64 70 80 90
20 30 40 50 60 70 80 90
20 30 40 50 60 70 80 90
20 30 40 40 60 70 80 90
40 30 40 50 60 70 80 90

4 5 6 6

20 30 40 50 60 70 80 91
20 30 40 50 60 70 80 92
20 30 40 50 60 70 80 93
2 30 40 50 64 70 80 90
20 30 20 50 60 70 80 90
20 30 40 50 60 70 80 90
20 30 40 40 60 70 80 90
40 30 40 50 60 70 80 90

4 5 1 6

20 30 40 50 60 70 80 91
20 30 40 50 60 70 80 92
20 30 40 50 60 70 80 93
20 30 40 50 64 70 80 90
20 30 40 50 60 70 80 90
20 30 40 50 60 70 80 90
20 1 40 40 60 70 80 90
40 30 40 50 60 70 80 90

4 5 6 1

我想从该文档中取出一个 8x8 矩阵数组和一个 1*4 矩阵数组是可能的,该怎么做?

【问题讨论】:

  • 您给定的数据用空格而不是逗号分隔。

标签: parsing csv matrix document maple


【解决方案1】:

下面生成一个 8x8 矩阵的表 M,和一个 1x4 行向量的表 V。

您可以选择将 M 和 V 预先创建为大小为 n 的数组。只需取消注释这些行。您可以看到它是针对 100 对 Matrix-Vector 扫描进行硬编码的。根据需要增加 n。当扫描下一个项目失败时,它会通过检测 fscanf 失败并跳出循环来停止。

我的示例使用了仅包含三对矩阵和向量的纯文本数据文件,并且在第四对扫描尝试失败时中断。

restart:
Z:="C://TEMP//mydata.txt":

fclose(Z);

#M:=Array(1..100):
#V:=Array(1..100):

for i from 1 to 100 do
   try
      M[i]:=fscanf(Z,"%{8,8}ldm")[1];
      V[i]:=fscanf(Z,"%{4}ldr")[1];
   catch "end of input encountered":
      break;
   end try;
end do;

M[2]; # returns the 2nd entry (a 8x8 Matrix) of M
V[2]; # returns the 2nd entry (a 1x4 row Vector) of V

【讨论】:

    猜你喜欢
    • 2010-11-29
    • 2013-09-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2010-11-06
    相关资源
    最近更新 更多