使用操作系统特定的换行符(CRLF、LF、CR)构建字符串,将其写入数据库表列
我想在数据库表的列中写入一个字符串,该字符串包含常用的新行字符R(使用操作系统特定的换行符(CRLF、LF、CR)构建字符串,将其写入数据库表列,r,newline,lf,R,Newline,Lf,我想在数据库表的列中写入一个字符串,该字符串包含常用的新行字符R(\n) 如何将新行转换为特定于操作系统的表示形式(Windows=CR/LF、Linux=LF、Mac=CR…) 我了解到R不提供特定于操作系统的表示,因此我必须找到解决方法: 打印/编辑字符串的任何尝试均失败: msg <- "I want to have \n a new line" cat(msg) # I want to have # a new line out <- capture.outpu
\n
)
如何将新行转换为特定于操作系统的表示形式(Windows=CR/LF、Linux=LF、Mac=CR…)
我了解到R不提供特定于操作系统的表示,因此我必须找到解决方法:
msg <- "I want to have \n a new line"
cat(msg)
# I want to have
# a new line
out <- capture.output(cat(msg))
out
# a vector with two elements (one for each row but no new line characters anymore)
# [1] "I want to have " " a new line"
paste(out, collapse = "\n") # how could I inject the correct new line characters here?
# [1] "I want to have \n a new line"
# welcome endless-loop :-)
msg在R中正确设置新行代码的一种方法是查询操作系统。由于OSX和Linux的行为方式相同,因此确定操作系统是否为Windows是一个问题。执行此操作的一种方法是查询OS
环境变量,如下所示
if(substr(Sys.getenv("OS"),1,7) == "Windows") {
# set Windows newline
newLine <- "\r\n"
}
else {
# set non-Windows newline
newLine <- "\n"
}
问候,
Len在R中正确设置新行代码的一种方法是查询操作系统。由于OSX和Linux的行为方式相同,因此确定操作系统是否为Windows是一个问题。执行此操作的一种方法是查询OS
环境变量,如下所示
if(substr(Sys.getenv("OS"),1,7) == "Windows") {
# set Windows newline
newLine <- "\r\n"
}
else {
# set non-Windows newline
newLine <- "\n"
}
问候,
Len在这里,您可以找到我的最终实现,作为接受答案的可能替代方案:
# Returns the operating system specific new line character(s):
# CR LF on Windows, else only LF...
# Simlar to Microsofts .Net "Environment.NewLine"
platform.NewLine <- function() {
is.windows <- grepl(tolower(.Platform$OS.type), "windows", fixed = TRUE)
if (is.windows) {
newline <- "\r\n"
} else {
newline <- "\n"
}
sys.name <- Sys.info()["sysname"]
is.windows.2nd.opinion <- grepl(tolower(sys.name), "windows", fixed = TRUE)
if (is.windows != is.windows.2nd.opinion)
warning("R seems to run on Windows OS but this could not be recognized for sure")
return(newline)
}
# Usage (examples) ------------------------------------------------------------------------------------------------
newline <- platform.NewLine()
# "print" shows the "symbolic" names (escape codes)
print(paste("Line1", "Line2", sep = newline))
# [1] "Line1\r\nLine2"
# uses "\n" or "\r\n" depending on your OS
# "cat" applies the newline escape codes to the output
cat(paste("Line1", "Line2", sep = newline))
# Line1
# Line2
#返回操作系统特定的新行字符:
#在Windows上使用CR LF,否则仅在。。。
#与Microsofts.Net“Environment.NewLine”类似
platform.NewLine在这里,您可以找到我的最终实现作为公认答案的可能替代方案:
# Returns the operating system specific new line character(s):
# CR LF on Windows, else only LF...
# Simlar to Microsofts .Net "Environment.NewLine"
platform.NewLine <- function() {
is.windows <- grepl(tolower(.Platform$OS.type), "windows", fixed = TRUE)
if (is.windows) {
newline <- "\r\n"
} else {
newline <- "\n"
}
sys.name <- Sys.info()["sysname"]
is.windows.2nd.opinion <- grepl(tolower(sys.name), "windows", fixed = TRUE)
if (is.windows != is.windows.2nd.opinion)
warning("R seems to run on Windows OS but this could not be recognized for sure")
return(newline)
}
# Usage (examples) ------------------------------------------------------------------------------------------------
newline <- platform.NewLine()
# "print" shows the "symbolic" names (escape codes)
print(paste("Line1", "Line2", sep = newline))
# [1] "Line1\r\nLine2"
# uses "\n" or "\r\n" depending on your OS
# "cat" applies the newline escape codes to the output
cat(paste("Line1", "Line2", sep = newline))
# Line1
# Line2
#返回操作系统特定的新行字符:
#在Windows上使用CR LF,否则仅在。。。
#与Microsofts.Net“Environment.NewLine”类似
事实上,这是我目前的工作方法(使用Sys.info()
),我本来希望R提供这样的功能OOTB,因为它能够在内部正确处理这个问题。@RYoda--这是R作为开源产品的特点之一。有时“明显的”功能没有实现。我刚刚接受了你的答案,但稍后也会发布我的个人解决方案作为单独的答案,使用稍微不同的方法。又来了!事实上,这是我目前的工作方法(使用Sys.info()
),我希望R能够提供这样的功能OOTB,因为它能够在内部正确处理这个问题。@RYoda——这是R作为开源产品的一个特点。有时“明显的”功能没有实现。我刚刚接受了你的答案,但稍后也会发布我的个人解决方案作为单独的答案,使用稍微不同的方法。又来了!