How to read quoted text containing escaped quotes

前端 未结 2 1583
陌清茗
陌清茗 2020-12-28 21:03

Consider the following comma separated file. For simplicity let it contain one line:


\'I am quoted\',\'so, can use comma inside - it is not separator h         


        
相关标签:
2条回答
  • 2020-12-28 21:20

    read_delim from package readr can handle escaped quotes, using the arguments escape_double and escape_backslash.

    read_delim(file, delim=',', escape_double=FALSE, escape_backslash=TRUE, quote="'")
    

    (Note older versions of readr do not support quoted newlines in CSV headers correctly: https://github.com/tidyverse/readr/issues/784)

    0 讨论(0)
  • 2020-12-28 21:37

    One possibility is to use readLines() to get everything read in as is, and then proceed by replacing the quote character by something else, eg :

    tt <- readLines("F:/temp/test.txt")
    tt <- gsub("([^\\]|^)'","\\1\"",tt) # replace ' by "
    tt <- gsub("\\\\","\\",tt) # get rid of the double escape due to readLines
    

    This allows you to read the vector tt in using a textConnection

    zz <- textConnection(tt)
    read.csv(zz,header=F,quote="\"") # give text input
    close(zz)
    

    Not the most beautiful solution, but it works (provided you don't have a " character somewhere in the file off course...)

    0 讨论(0)
提交回复
热议问题