julia writedlm 在字符串中包含引号时添加额外的引号

huangapple go评论127阅读模式
英文:

julia writedlm adding extra quotes when there are quotes within a string

问题

我正在将文件读入字符串列表,使用换行符作为分隔符。我更改了其中一些行,然后将该列表写回文件。这会在某些行上添加额外的引号。

这可能是`writedlm`中的一个错误。大多数文件都是按预期行为写入的。当字符串中包含引号时,它似乎会出错。知道为什么会发生这种情况,如何修复它,或者如果这是一个错误,应该报告到哪里吗?

代码:

file = "og_file.F"
new_file = "new_file.F"
f = read(file, String)
meta = split(f, "\n")
writedlm(new_file, meta)


原始文件:

#include "DARWIN_OPTIONS.h";

CBOP
C !ROUTINE: DARWIN_PLANKTON
C !INTERFACE: ==========================================================
SUBROUTINE DARWIN_PLANKTON(
I Ptr,
U gTr,
O chlout, diags,
I PAR, photoTempFunc, hetTempFunc, grazTempFunc, reminTempFunc,
I mortTempFunc, mort2TempFunc, uptakeTempFunc,
#ifdef DARWIN_DEBUG
I iG, jG, k, dT,
#endif
I myTime,myIter,myThid)

C !DESCRIPTION:

C !USES: ===============================================================
IMPLICIT NONE
#ifdef ALLOW_RADTRANS
#include "RADTRANS_SIZE.h";
#endif
#include "DARWIN_SIZE.h";
#include "DARWIN_INDICES.h";
#include "DARWIN_DIAGS.h";
#include "DARWIN_RADTRANS.h";
#include "DARWIN_PARAMS.h";
#include "DARWIN_TRAITS.h";


输出文件:

"#include ""DARWIN_OPTIONS.h"";"

CBOP
C !ROUTINE: DARWIN_PLANKTON
C !INTERFACE: ==========================================================
SUBROUTINE DARWIN_PLANKTON(
I Ptr,
U gTr,
O chlout, diags,
I PAR, photoTempFunc, hetTempFunc, grazTempFunc, reminTempFunc,
I mortTempFunc, mort2TempFunc, uptakeTempFunc,
#ifdef DARWIN_DEBUG
I iG, jG, k, dT,
#endif
I myTime,myIter,myThid)

C !DESCRIPTION:

C !USES: ===============================================================
IMPLICIT NONE
#ifdef ALLOW_RADTRANS
"#include ""RADTRANS_SIZE.h"";"
#endif
"#include ""DARWIN_SIZE.h"";"
"#include ""DARWIN_INDICES.h"";"
"#include ""DARWIN_DIAGS.h"";"
"#include ""DARWIN_RADTRANS.h"";"
"#include ""DARWIN_PARAMS.h"";"
"#include ""DARWIN_TRAITS.h"";"


<details>
<summary>英文:</summary>

I&#39;m reading a file into a list of strings using new-lines as separators. I&#39;m changing a few of the lines, then writing that list back to the file. This adds in extra quotation marks only on certain lines. 

This might be a bug in `writedlm`. Most of the file is written with the expected behavior. It appears to mess up when there are quotes within the string. Any idea why this is happening, how to fix it, or where to report this if it&#39;s a bug? 

Code: 

file = "og_file.F"
new_file = "new_file.F"
f = read(file, String)
meta = split(f, "\n")
writedlm(new_file, meta)


Original file: 

#include "DARWIN_OPTIONS.h"

CBOP
C !ROUTINE: DARWIN_PLANKTON
C !INTERFACE: ==========================================================
SUBROUTINE DARWIN_PLANKTON(
I Ptr,
U gTr,
O chlout, diags,
I PAR, photoTempFunc, hetTempFunc, grazTempFunc, reminTempFunc,
I mortTempFunc, mort2TempFunc, uptakeTempFunc,
#ifdef DARWIN_DEBUG
I iG, jG, k, dT,
#endif
I myTime,myIter,myThid)

C !DESCRIPTION:

C !USES: ===============================================================
IMPLICIT NONE
#ifdef ALLOW_RADTRANS
#include "RADTRANS_SIZE.h"
#endif
#include "DARWIN_SIZE.h"
#include "DARWIN_INDICES.h"
#include "DARWIN_DIAGS.h"
#include "DARWIN_RADTRANS.h"
#include "DARWIN_PARAMS.h"
#include "DARWIN_TRAITS.h"


output file: 

"#include ""DARWIN_OPTIONS.h"""

CBOP
C !ROUTINE: DARWIN_PLANKTON
C !INTERFACE: ==========================================================
SUBROUTINE DARWIN_PLANKTON(
I Ptr,
U gTr,
O chlout, diags,
I PAR, photoTempFunc, hetTempFunc, grazTempFunc, reminTempFunc,
I mortTempFunc, mort2TempFunc, uptakeTempFunc,
#ifdef DARWIN_DEBUG
I iG, jG, k, dT,
#endif
I myTime,myIter,myThid)

C !DESCRIPTION:

C !USES: ===============================================================
IMPLICIT NONE
#ifdef ALLOW_RADTRANS
"#include ""RADTRANS_SIZE.h"""
#endif
"#include ""DARWIN_SIZE.h"""
"#include ""DARWIN_INDICES.h"""
"#include ""DARWIN_DIAGS.h"""
"#include ""DARWIN_RADTRANS.h"""
"#include ""DARWIN_PARAMS.h"""
"#include ""DARWIN_TRAITS.h"""


</details>


# 答案1
**得分**: 1

以下是您要的内容的翻译:

这是有意的。原因是`writedlm`旨在写入表格数据(每行具有多个字段)。在这种情况下,标准行为是使用双引号`&quot;`来包装包含它们的字段,请参考https://en.wikipedia.org/wiki/Comma-separated_values:

&gt;更复杂的CSV实现允许它们,通常需要在包含保留字符(如逗号、双引号或较不常见的换行符)的值周围使用&quot;(双引号)字符。然后,嵌入的双引号字符可以由一对连续的双引号表示。

对于您的用例,最好使用如下方式:
``` file = "og_file.F"
new_file = "new_file.F"
meta = readlines(file)
open(new_file, "w") do io
    foreach(line -> println(io, line), meta)
end
英文:

This is intended. The reason is that writedlm is intended to write tabular data (having multiple fields per one line). In this case a standard behavior is to use double quotes &quot; to wrap fields that contain them, see https://en.wikipedia.org/wiki/Comma-separated_values:

> More sophisticated CSV implementations permit them, often by requiring " (double quote) characters around values that contain reserved characters (such as commas, double quotes, or less commonly, newlines). Embedded double quote characters may then be represented by a pair of consecutive double quotes

For your use case instead it is better to use e.g.:

file = &quot;og_file.F&quot;
new_file = &quot;new_file.F&quot;
meta = readlines(file)
open(new_file, &quot;w&quot;) do io
    foreach(line -&gt; println(io, line), meta)
end

答案2

得分: 0

I solved it by adding in the keyword arg for quotes (based off of this answer from julia hub). There is no easily accessible documentation about it, had to look at the source code for writedlm.

writedlm(new_file, meta, quotes=false)
英文:

Thanks for explaining why this is the expected behavior!

I solved it by adding in the keyword arg for quotes (based off of this answer from julia hub). There is no easily accessible documentation about it, had to look at the source code for writedlm.

writedlm(new_file, meta, quotes=false)

huangapple
  • 本文由 发表于 2023年4月7日 01:08:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定