如何在写csv时在R中指定quotechar ?

时间:2022-09-15 15:11:58

The Python library Pandas allows to specify a character used to quote fields in the DataFrame.to_csv function.

Python库允许指定用于在DataFrame中引用字段的字符。to_csv函数。

I'd like to use a similar function in R to use single quote instead of double quotes to quote non-numeric field. Is there a way to do so in R ?

我想在R中使用一个类似的函数来使用单引号而不是双引号来引用非数值字段。在R中有这样的方法吗?

I've tried the write.table and write.csv functions and I didn't found this option so I was wondering if there is a R package to do so.

我试着写。表和写作。csv函数,我没有找到这个选项,所以我想知道是否有一个R包。

1 个解决方案

#1


2  

This writes out the first few rows of iris replacing each double quote with single quote:

这就写出了iris的前几行,将每个双引号替换为单引号:

writeLines(gsub('"', "'", capture.output(write.csv(head(iris), row.names =  FALSE))))

giving:

给:

'Sepal.Length','Sepal.Width','Petal.Length','Petal.Width','Species'
5.1,3.5,1.4,0.2,'setosa'
4.9,3,1.4,0.2,'setosa'
4.7,3.2,1.3,0.2,'setosa'
4.6,3.1,1.5,0.2,'setosa'
5,3.6,1.4,0.2,'setosa'
5.4,3.9,1.7,0.4,'setosa'

As long as the data itself has no quotes it should be OK.

只要数据本身没有引号就可以。

#1


2  

This writes out the first few rows of iris replacing each double quote with single quote:

这就写出了iris的前几行,将每个双引号替换为单引号:

writeLines(gsub('"', "'", capture.output(write.csv(head(iris), row.names =  FALSE))))

giving:

给:

'Sepal.Length','Sepal.Width','Petal.Length','Petal.Width','Species'
5.1,3.5,1.4,0.2,'setosa'
4.9,3,1.4,0.2,'setosa'
4.7,3.2,1.3,0.2,'setosa'
4.6,3.1,1.5,0.2,'setosa'
5,3.6,1.4,0.2,'setosa'
5.4,3.9,1.7,0.4,'setosa'

As long as the data itself has no quotes it should be OK.

只要数据本身没有引号就可以。