将带有逗号和控制字符的字符串写入CSV文件中。

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

write string with comma and control characters to CSV

问题

我有一个以如下格式的 string

val data = "some string\n , other \n string \t";

我想要将上述字符串写入到一个单列单元格中,就像它原本的样子。

因此,控制字符不应该产生任何影响,而且逗号不应该导致字符串被拆分为两个独立的CSV列。所以,我想要在CSV中写入的是纯粹的原始数据:

some string\n , other \n string \t

但是,我得到的结果却是分开的形式:

some string

and

other
string

到目前为止,我尝试过:

尝试1:\"\\" + data + "\\\""

尝试2:raw\"$data\"

尝试3:s\"\"\"$data\"\"\"

但是似乎什么都不起作用。

英文:

I have a string in the format:

val data = "some string\n , other \n string \t"

I want to write the above string to a single column in a single cell, as it is.

So, control characters should not have any effect and comma should not cause the string to be split into two separate CSV columns. So, what I want to be written in CSV is plain raw data:

some string\n , other \n string \t

But, what I get instead is in separated form:

some string

and

 other
string

So, far I have tried:

Attempt 1: "\"" + data + "\""

Attempt 2: raw"$data"

Attempt 3: s"""$data"""

But nothing seems to work.

答案1

得分: 2

如何将你写出的数据显示在列和行中取决于读取CSV的方式。从你的问题中我理解你想要写出一些内容,以便在渲染的CSV中按原样显示"some string\n , other \n string \t"。为此,在大多数CSV的“变种”中——CSV的规范性差异大,定义不一致——你需要对字符串进行引用,并转义控制字符。

所以你最终会得到类似这样的结果:

val escaped = data.replace("\n", "\\n").replace("\t", "\\t")
val quoted = "\"" + escaped + "\""

然后你可以使用 quoted

可能还有其他字符需要替换。当这变得太难以临时完成时,你可能需要使用一个能为你进行引用和转义的CSV库。在这种情况下,你可能想看看 Kantan:https://nrinaudo.github.io/kantan.csv/。

英文:

How the data you write out is displayed in columns and rows depends on whatever is reading the CSV. What I understand from your question is that you want to write out something that will display "some string\n , other \n string \t" as is in the rendered CSV. For that, in most "flavors" of CSV -- CSV is notoriously poorly and inconsistently specified -- you'll need to quote the string, and escape the control characters.

So you'll end up with something like

val escaped = data.replace("\n", "\\n").replace("\t", "\\t")
val quoted = "\"" + escaped + "\""

and you use quoted.

There may be more characters you need to replace. When this becomes too unwieldy to do ad-hoc, you may want to use a CSV library that does the quoting and escaping for you. In that case, you might want to take a look at Kantan: https://nrinaudo.github.io/kantan.csv/

答案2

得分: 1

变量data初始化字符串字面值中的\n不是两个字符,而是一个字符(换行符)。写入文件的换行符是分隔两行的(比这更复杂一些 - 不同的主题)。如果您真的想要在文件中得到一个后跟'n'的反斜杠字符,您需要将换行符转换为这两个字符。

例如:outWriter.write(data.replaceAll("\n", "\\n"))

请注意,替换部分有双反斜杠,这是创建反斜杠字符的方式。

您还需要对任何其他控制字符进行取消转义。(可能有一些开源库已经具有此功能)

或者

您可以对字符串字面值使用双反斜杠,这样可以原样写入。

注意:这被标记为Java问题,所以我给出了一个Java答案。我不知道有一个使用逗号作为行分隔符的Java写法。这是Scalia的特点吗?(另一个标签)

英文:

The \n you see in the string literal initializing variable data is not two characters but one (a newline). A newline written to a file is what separates two lines (its a bit more complicated than that - different topic). If you really want a backslash character followed by a 'n' in the file, you need to convert the newline into those two characters.

e.g.: outWriter.write(data.replaceAll("\n", "\\n"))

Note that the replacement has the double-backslash which is how a backslash character is created.

You will also need to un-escape any other control characters too. (There is probably some open-source library that has this already)

OR

You can double-backslash the string literals, so it can be written as-is.

Note: This was tagged Java, so I gave a Java answer. I don't know a Java write that uses commas as line separators. Is that a Scalia thing? (the other tag)

答案3

得分: 1

import scala.reflect.runtime.universe.{Constant, Literal}

val data = "some string\n , other \n string \t"
def escape(raw: String): String =
    Literal(Constant(raw)).toString

escape(data)

escape prints -> "some string\n , other \n string \t"

This should work for you
英文:
import scala.reflect.runtime.universe.{Constant, Literal}

val data = "some string\n , other \n string \t"
def escape(raw: String): String =
    Literal(Constant(raw)).toString

escape(data)

escape prints -> "some string\n , other \n string \t"

This should work for you

huangapple
  • 本文由 发表于 2020年10月19日 21:40:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64428602.html
匿名

发表评论

匿名网友

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

确定