如何以字符串形式读取txt文件

时间:2019-07-11 15:20:56

标签: r

有一个txt文件名为“ text.sql”

它包括:

SELECT '[?D?]' as day_num,* FROM public.aabbccdddd
where "RSRP" like '%-90dBm'

我想读取此文件并获取字符串,用于sql查询。

就像python代码一样:

        f= open(SQL_File_Name, 'r')
        sql_string = f.read()
        f.close()

我尝试过:

  txt <- readLines(i)
  txt <- paste(txt,collapse = " ")

,但不能使用dbGetQuery函数。


# day month number
dayNum<-as.numeric(difftime(as.character(as.Date(Sys.Date(),tz = Sys.timezone())), "1970-01-01",units="days"))-1
monNum<-(as.numeric(format(Sys.Date(),'%Y'))-1970)*12+as.numeric(format(Sys.Date(),'%m'))-1

# read query save
list <- list.files(pattern = "*.sql$")
for (i in list) {
  txt <- readLines(i)
  txt <- paste(txt,collapse = " ")
  filename <- str(i)
  table_name <- gsub(".sql","",filename)
  sql <-gsub("[?D?]",dayNum,txt)
  datadf <- dbGetQuery(postgres_conn,sql)
  logdf = data.frame(type = table_name, 
         taste = sql, 
         color = filename, 
         stringsAsFactors = FALSE) 
  dbWriteTable(postgres_conn, name = c("public",table_name), value = datadf ,append=TRUE,row.names=FALSE,overwrite=FALSE)
  dbWriteTable(postgres_conn, name = c("public","fruits"), value = dt2insert,append=TRUE,row.names=FALSE,overwrite=FALSE)
}

0 个答案:

没有答案