禁用双引号

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

Disable double quotes

问题

The code you provided is adding double quotes around the data because of the "quoteAll" option set to true. To achieve the desired format without double quotes, you can set the "quoteAll" option to false as follows:

df_test.repartition(1).write.option('header', 'false').option("delimiter", '|').option("quoteAll", 'false').mode("overwrite").csv(path_of_file)

This should result in the data in the file appearing as:

A|1
B|2

Please make sure to update your code accordingly.

英文:

When I use the below code, it is adding double quotes at the starting and end of the data. Ideally, this is valid as I am telling it is a pipe delimeted file and pipe is coming in the data. But I have a requirement not to get double quote added in the starting and end of the data. Is there a way to acheive this?

data = [{"Cnt": 'A|1'},{"Cnt": 'B|2'}]
rdd = sc.parallelize(data)
df_test = rdd.toDF()   
df_test.repartition(1).write.option('header','false').option("delimiter",'|').option("quoteAll", 'false').option("quote", None).mode("overwrite").csv(path_of_file)

Data in the file looks like below after exporting

"A|1"
"B|2"

But I need the data in the file like below.

A|1
B|2

Apache Spark version - 3.3.1

答案1

得分: 2

I reproduced the same code what you have done, even i got the data in quotes as shown below

Later, I tried saving in text format then i got output without quotes.

data = [{"Cnt": 'A|1'}, {"Cnt": 'B|2'}]
rdd = sc.parallelize(data)
df_test = rdd.toDF()
df_test.repartition(1).write.option('header','false').option("delimiter",'|').option("quoteAll", 'false').option("quote", None).mode("overwrite").text("/demot/")

英文:

I reproduced the same code what you have done,
even i got the data in quotes as shown禁用双引号 below

Later, I tried saving in text format then i got output without quotes.

data = [{"Cnt": 'A|1'},{"Cnt": 'B|2'}]
rdd = sc.parallelize(data)
df_test = rdd.toDF()
df_test.repartition(1).write.option('header','false').option("delimiter",'|').option("quoteAll", 'false').option("quote", None).mode("overwrite").text("/demot/")

禁用双引号

huangapple
  • 本文由 发表于 2023年4月13日 16:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003574.html
匿名

发表评论

匿名网友

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

确定