将值附加到导出的 .dat 的最快方法

发布于 2024-12-29 21:06:49 字数 845 浏览 5 评论 0原文

我目前正在尝试编写一个小型导出函数,但是在尝试获取正确的导出格式时遇到了问题。 LoadElements 函数仅生成系统列表(包含 200 个配对元素)。

elements = Table[LoadElements[X], {10}];
Export["C:\\Exports\\elements.dat", elements]

这样我就得到了所需的格式并且功能很快:对于每个列表一个新行;示例:
{列表1元素1,列表1元素2...}
{列表2元素1,列表2元素2...}
问题是:使用导出功能,我无法将新值附加到文件中。

比我尝试使用流:但是:1.它比导出代码慢得多,2.我没有获得正确的格式:

Do[
file = OpenAppend["C:\\Exports\\elements.dat", PageWidth -> Infinity];
Write[file, elemnts = Table[LoadElements[X], {2}]];
Close[file];
, {2}];

需要PageWidth Infinity,因为否则会出现换行符,但它给了我以下格式:
{列表1}{列表2}
{列表3}{列表4}
而且速度相当慢..

我将感谢一个想法,如何能够轻松地将值附加到具有所需表格格式的文件中..


非常感谢,它通常有效。

不幸的是,这会在每对值之间产生一个额外的空换行符,知道为什么吗? 所以这会产生:
结果对

结果对
而不是:
结果对
结果对
我的意思是我可以用另一个脚本手动删除空行,但是直接获得正确的输出会很好:)

I'm currently trying to write a small export function, however I reached a problem when trying to get the correct export format.
The LoadElements functions just generates systematic Lists (of 200 paired elements).

elements = Table[LoadElements[X], {10}];
Export["C:\\Exports\\elements.dat", elements]

Like that i get the desired format and the function is fast: For every List a new Line; Example:
{List1Element1, List1Element2...}
{List2Element1, List2Element2...}
The Problem is: With the Export function I cannot Append new valus to the File.

Than I tried to work with streams: However: 1. It is substantially slower than the Export code, 2. I didn't achieve to get the correct Format:

Do[
file = OpenAppend["C:\\Exports\\elements.dat", PageWidth -> Infinity];
Write[file, elemnts = Table[LoadElements[X], {2}]];
Close[file];
, {2}];

The PageWidth Infinity was needed because otherwise there were Line Breaks, but it gives me the Following Format:
{List1}{List2}
{List3}{List4}
and it is quite slow..

I would be thankfull for an Idea how to be able to easily Append Values to a File with the required Table like format..


thank you a lot, it generally works.

Unfortunately, this produces an additional empty line break between every pair of values, any idea why?
So this produces:
Resultpair

Resultpair
indstead of:
Resultpair
Resultpair
I mean I can manually get rid of the empty line with another script, however it would be nice to directly get the correct output :)

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

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

发布评论

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

评论(1

眼角的笑意。 2025-01-05 21:06:49

您可以考虑结合使用 ExportString (因为 Export 已经生成了您想要的格式)和 WriteString

elements1 = RandomInteger[99, {15, 2, 5}];

elements2 = RandomInteger[99, {15, 3, 3}];

Export["elements.dat", elements1]

file = OpenAppend["elements.dat"];
WriteString[file, "\n", ExportString[elements2, "Table"]]
Close[file]

You may consider a combination of ExportString (since Export is already producing the format you desire) and WriteString:

elements1 = RandomInteger[99, {15, 2, 5}];

elements2 = RandomInteger[99, {15, 3, 3}];

Export["elements.dat", elements1]

file = OpenAppend["elements.dat"];
WriteString[file, "\n", ExportString[elements2, "Table"]]
Close[file]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文