如何使fwrite()不加双引号? [英] How to get fwrite() not to double quotes?

查看:67
本文介绍了如何使fwrite()不加双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fread 读取了在其字段中包含html代码的csv文件,对其进行了一些维护,然后使用 fwrite 将生成的数据表写入文件.问题是现在所有的html都用四引号填充,例如 colspan =""" 7"" .有 qmethod 参数,但是我不确定如何使用它,因为我也不想用反斜杠转义引号.

I read a csv file containing html code in its fields using fread, do some maintenance on it and write the resulting datatable to a file using fwrite. Problem is that now all the html is filled with quadruple quotes such as colspan="""""7"""". There is the qmethod argument but I'm not sure how to use it as I'm not interested to escape quotes with backslash either.

是否有可能避免这种情况,或者我必须使用sed吗?

Is it possible to avoid that or do I have to use sed ?

例如,我得到了csv文件:

For instance I got the csv file :

htmlcode,id
<colspan="7">,1
<colspan="8">,2

我使用 fread 将其读取到数据表中,然后删除id列,并使用 fwrite 将数据表写回到csv文件中,得到:

I read that into a datatable with fread, I remove the id column and write the datatable back into a csv file with fwrite I get:

htmlcode
<colspan=""""7"""">
<colspan=""""8"""">

推荐答案

两种可能的解决方案(使用 data.table v1.10.0 ):

Two possible solutions (using v1.10.0 of data.table):

1::使用 quote = FALSE 参数:

fwrite(dt, 'fwrite.csv', quote = FALSE)

在文本编辑器中打开文件时,您将看到以下内容:

When opening the file in a text editor, you will see this:

htmlcode
<colspan="7">
<colspan="8">

2:(用评论中的@ joel.wilson推荐)将双括号替换为单括号:

2: Replace the double brackets with single one (as also suggeted by @joel.wilson in the comments):

dt[, htmlcode := gsub('\"', '\'', htmlcode)]
fwrite(dt, 'fwrite.csv')

在文本编辑器中打开文件时,您将看到以下内容:

When opening the file in a text editor, you will see this:

htmlcode
<colspan='7'>
<colspan='8'>

这篇关于如何使fwrite()不加双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆