为什么 Maple 在读取逗号/空格分隔值时失败以及如何修复它?
我正在尝试解析数据,例如:
29 28 23 19 14 11 13 17
36 26 21 21 13 8 7 6
54 33 25 26 18 13 10 3
70 45 29 26 20 19 16 6
85 63 41 27 22 23 20 13
94 82 58 36 27 25 20 18
93 91 76 53 36 26 21 18
94 96 90 71 47 32 26 26
1.60721 -0.529301 6.88206 5.14482
27 30 32 34 37 40 39 36
6 8 10 10 12 13 14 13
2 1 2 3 4 5 7 10
3 1 2 3 5 6 7 12
5 1 1 3 4 4 7 10
11 6 3 3 5 6 8 10
12 9 4 3 5 8 11 14
16 14 11 10 13 18 24 28
0.709391 6.50125 0.745197 0.4955
39 38 37 39 40 40 40 40
20 21 21 22 23 23 24 24
14 16 17 17 16 17 18 18
12 12 13 12 12 13 13 14
12 12 12 12 12 12 11 10
13 11 11 13 14 14 13 13
17 16 17 20 22 21 21 22
34 33 34 39 40 38 40 44
6.36007 0.492539 6.03537 6.58187
使用如下算法:
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
但它失败并出现错误,(在 fscanf 中)读取 Vector 时遇到无效字符 .
如何修复此类错误?
I am trying to parse data such as:
29 28 23 19 14 11 13 17
36 26 21 21 13 8 7 6
54 33 25 26 18 13 10 3
70 45 29 26 20 19 16 6
85 63 41 27 22 23 20 13
94 82 58 36 27 25 20 18
93 91 76 53 36 26 21 18
94 96 90 71 47 32 26 26
1.60721 -0.529301 6.88206 5.14482
27 30 32 34 37 40 39 36
6 8 10 10 12 13 14 13
2 1 2 3 4 5 7 10
3 1 2 3 5 6 7 12
5 1 1 3 4 4 7 10
11 6 3 3 5 6 8 10
12 9 4 3 5 8 11 14
16 14 11 10 13 18 24 28
0.709391 6.50125 0.745197 0.4955
39 38 37 39 40 40 40 40
20 21 21 22 23 23 24 24
14 16 17 17 16 17 18 18
12 12 13 12 12 13 13 14
12 12 12 12 12 12 11 10
13 11 11 13 14 14 13 13
17 16 17 20 22 21 21 22
34 33 34 39 40 38 40 44
6.36007 0.492539 6.03537 6.58187
With an algorithm such as:
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
but it fails with Error, (in fscanf) invalid character .
encountered while reading Vector
How can I fix such an error ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在代码行中,
格式修饰符“ldr”表示进入行 (r) 向量的 ld(长整数)值。您收到错误,因为数据包含浮点数,并且该代码是专门为期望整数而编写的。 (您之前关于此主题的文章包含所有整数值数据。)
如果您现在有一个文本文件,其中包含一些浮点数,那么您可以将用于处理矢量数据的代码行更改为其中任何一个,
或者
或
因为 % g、%e 和 %f 是 scanf 的浮点格式修饰符。
如果您不知道文件中的条目类型,您也可以尝试,
因为在 Maple 的 scanf 中 %a 描述符意味着“代数”(包括整数和浮点数以及许多其他类型)。
请参阅 Maple 自己的帮助系统中的帮助页面 ?scanf 或在线访问
扫描面
宏碁
In the line of code,
the format modifier "ldr" means ld (long integer) values going into a row (r) Vector. You got an error because the data contains floats, and that code was written to expect integers specifically. (Your previous post on this topic had all integer-valued data.)
If you now have a text file with some floats in it, then you could change the that code line for handling the Vector data to any of these,
or
or
since %g, %e, and %f are float format modifiers for scanf.
If you don't know what sort of entries are in the file, you could also try,
since in Maple's scanf the %a descriptor means "algebraic" (which includes integers and floats, amongst many other types).
See the help-page ?scanf in Maple's own help system or online at,
scanf
acer