将数据从 txt 导入到 Mathematica

发布于 2024-11-11 13:43:23 字数 844 浏览 6 评论 0原文

考虑mathematica 中的以下列表:

a = {
   {
    {0, 0, 0}, {1, 0, 0}, {1, 1, 0}
    },
   {
    {0, 0, 1}, {1, 0, 1}, {1, 1, 1}
    }
   };

现在,调用:

Export["test.dat", a]

然后

b = Import["test.dat"]

您将看到最后的a 不等于b。我应该将此视为一个功能还是一个错误?

此外,我想导入具有以下格式的列表: {P1,P2,P3...,Pn} 其中 Pi={v1,v2,v3,..., vm} 和每个 vi={x,y,z},其中 x,y,z 是表示顶点 vi< 坐标的数字/代码>。这应该是一个多边形列表。

我应该如何设置我的 .dat 文件以便我可以使用 Mathematica 读取它,以及我应该如何读取它?我尝试模仿上面 Export["test.dat",a] 的输出,但后来我发现了另一个问题。我发现这个问题 ,但无法让答案对我有用......

有什么想法吗?提前致谢!

Consider the following list in mathematica:

a = {
   {
    {0, 0, 0}, {1, 0, 0}, {1, 1, 0}
    },
   {
    {0, 0, 1}, {1, 0, 1}, {1, 1, 1}
    }
   };

Now, invoke:

Export["test.dat", a]

and then

b = Import["test.dat"]

You will see that at the end a doesn't equal b. Should I consider this as a feature or a bug?

Furthermore, I would like to import a list having the following format: {P1,P2,P3...,Pn} where Pi={v1,v2,v3,...,vm} and each vi={x,y,z} where x,y,z are numbers representing the coordinates of the vertex vi. This should be a list of polygons.

How should I set my .dat file so I can read it with Mathematica, and how should I read it? I tried to imitate the output of Export["test.dat",a] above, but then I discovered the other issue. I found this question, but couldn't make the answer work for me...

Any ideas? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小情绪 2024-11-18 13:43:23

您应该指定导入/导出所需的确切格式,否则 Mathematica 可能无法猜测正确的格式。

所以你的问题归结为什么文本格式适合存储 3D 数组?

如果您使用 Mathematica,最简单的方法可能是使用 Mathematica 的表达式语法导出表达式,即 Export["data.m", a, "Package"]。这种格式相对容易用其他语言编写(但解析起来并不容易)。您的另一个选择是为您的 3D 数据创建一些新的易于解析的文本格式,并用 Mathematica 和您需要使用的其他语言为其编写您自己的输入/输出函数。

由于您正在使用的数据的格式是固定的(您总是有坐标三元组),最简单的解决方案可能是在导出之前展平列表,并在导入后对其进行分区,如下所示:

Export["test.txt", Join @@@ a, "Table"]
b = Import["text.txt", "Table"]
Partition[#, 3]& /@ a

You should specify the exact format that you need to import/export in, otherwise Mathematica might not be able to guess the correct format.

So your question boils down to what textual format is suitable for storing 3D arrays?

If you work with Mathematica, probably the easiest thing to do is exporting the expression using Mathematica's expression syntax, i.e. Export["data.m", a, "Package"]. This format is relatively easy to write from other languages (but it's not as easy to parse). Your other option would be to make up some new easy to parse textual format for your 3D data, and write your own input/output functions for it in both Mathematica and the other languages you need to work with.

Since the format of the data you are working with is fixed (you always have coordinate triplets), the easiest solution may be to just flatten out your list before exporting, and partition it after importing, like this:

Export["test.txt", Join @@@ a, "Table"]
b = Import["text.txt", "Table"]
Partition[#, 3]& /@ a
思念满溢 2024-11-18 13:43:23

为了存储 MMA 表达式,我建议使用 DumpSave (二进制,取决于系统)、SavePut,但如果您想使用 Export I' d 将 a 转换为字符串,并将其导出为文本。 (我在下面使用 ImportStringExpertString,因此我不需要文件,但它对于 Import的工作原理相同导出)。在我看来,这坚如磐石。

a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
b = ToExpression@ImportString[ExportString[a // ToString, "Text"], "Text"]

(*  ==>
{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)

a == b

(* ==> True *)

读取多边形列表应该具有相同的效果:

b = ToExpression@ImportString["test.dat", "Text"]

For storing MMA expression I'd suggest DumpSave (binary, system dependent),Save or Put, but if you want to use Export I'd convert a to a string , and export that as text. (I use ImportString and ExpertString below, so that I don't need a file, but it works the same for Import and Export). IMO this is solid as a rock.

a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
b = ToExpression@ImportString[ExportString[a // ToString, "Text"], "Text"]

(*  ==>
{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)

a == b

(* ==> True *)

Reading your polygon list should work the same:

b = ToExpression@ImportString["test.dat", "Text"]
孤独患者 2024-11-18 13:43:23

例如,您还可以这样做:

a={{{0,0,0},{1,0,0},{1,1,0}},{{0,0,1},{1,0,1},{1,1,1}}};

Export["c:\\test.dat",a,"MathML"];
b=ToExpression@Import["c:\\test.dat","MathML"]

(*
->{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)

额外的好处是此方法不需要解析 Import 输出

You may also do for example:

a={{{0,0,0},{1,0,0},{1,1,0}},{{0,0,1},{1,0,1},{1,1,1}}};

Export["c:\\test.dat",a,"MathML"];
b=ToExpression@Import["c:\\test.dat","MathML"]

(*
->{{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}}
*)

The additional benefit is that this method does not require parsing the Import output

|煩躁 2024-11-18 13:43:23

我也去这个问题。我的解决方案如下:

IN[]: a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
      Export["test.dat", a, "List"];
      b = ToExpression@Import["test.dat", "List"];
      a == b

Out[]: True

希望这有帮助。此致。

I also go this Problem. My solution is the following:

IN[]: a = {{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}}, {{0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
      Export["test.dat", a, "List"];
      b = ToExpression@Import["test.dat", "List"];
      a == b

Out[]: True

Hope this helps. Best regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文