我正在处理包含印地语文本的文件并进行解析。我在Rstudio中编写了代码,并且成功地执行了它。但现在,我需要使用R.exe/Rscript.exe从命令行执行相同的脚本,但它不能以同样的方式工作。我已经从RStudio和终端运行了一个简单的脚本:
n_p<-'नाम'
Encoding(n_p)
gregexpr(n_p,c('adfdafc','नाम adsfdfa'))
sessionInfo()
RStudio中的输出:
> n_p<-'नाम'
>
> Encoding(n_p)
[1] "UTF-8"
>
> gregexpr(n_p,c('adfdafc','नाम adsfdfa'))
[[1]]
[1] -1
attr(,"match.length")
[1] -1
[[2]]
[1] 1
attr(,"match.length")
[1] 3
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7600)
Matrix products: default
locale:
[1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C
[5] LC_TIME=English_India.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rJava_0.9-10
loaded via a namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0
在cmd中使用R.exe输出(用于调试目的。Rscript.exe会给出类似的输出,如果不是完全相同的话)。
> n_p<-'à☼"à☼_à☼r'
>
> Encoding(n_p)
[1] "latin1"
>
> gregexpr(n_p,c('adfdafc','à☼"à☼_à☼r adsfdfa'))
[[1]]
[1] -1
attr(,"match.length")
[1] -1
[[2]]
[1] 1
attr(,"match.length")
[1] 9
> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7600)
Matrix products: default
locale:
[1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C
[5] LC_TIME=English_India.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.0
我尝试更改语言环境,但是
Sys.setlocale
拒绝正常工作。在某些情况下,gregexpr
在无法解析非 ASCII 代码时会出现错误。最后,即使没有错误运行,它也不能正确匹配正则表达式。目前我无法提供可重现的示例,但稍后我会尝试。请帮忙。
options(encoding = "UTF-8")
。 - Ian Wesleyeval(parse(filename, encoding="UTF-8"))
- wp78destringr
包中的str_conv
函数(详细信息请点击此处)在您的R脚本中进行转换。当我通过参数将字符串传递给R脚本时,这个函数对我来说至少是有效的。 - undefined