将值附加到导出的 .dat 的最快方法
我目前正在尝试编写一个小型导出函数,但是在尝试获取正确的导出格式时遇到了问题。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以考虑结合使用
ExportString
(因为Export
已经生成了您想要的格式)和WriteString
:You may consider a combination of
ExportString
(sinceExport
is already producing the format you desire) andWriteString
: