【问题标题】:How to fix a problem with Cyrillic symbols (cmd system commands)?如何解决西里尔符号(cmd 系统命令)的问题?
【发布时间】:2021-11-10 01:32:24
【问题描述】:

在我之前的 post 中,我提出了一个关于 R 中西里尔符号的问题。 今天我又遇到了一个。 例如,我们想查看我们正在运行的进程:

    test <- system2(command="tasklist",
                   stdout=TRUE,
                   stderr=TRUE, 
                   wait = TRUE)

我们所看到的......

 [1] ""                                                                            
 [2] "€¬п ®Ўа §                      PID €¬п бҐббЁЁ          ь ᥠ­б        Џ ¬пвм"
 [3] "========================= ======== ================ =========== ============"
 [4] "System Idle Process              0 Services                   0        24 ЉЃ"
 [5] "System                           4 Services                   0       580 ЉЃ"
 ***

"Iconv",在上一个任务中提供了帮助 - 在此处无法提供帮助。

sys.setlocale - 也是。

什么可以解决这个问题?

【问题讨论】:

  • 命令chcp的输出是什么?
  • @lit 我默认有 - 866 从控制台输出到 txt 文件也有不可读的字母。当我将 chcp 更改为 1251 时 - 文件中的文本可以读取,但 RStudio 控制台中的输出再次损坏。 chcp 65001 - 列已翻译成英文(控制台和输出文件;RStudio 控制台仍有“mojibake”)。
  • 如果 tasklist.execmd.exe 提示符或 PowerShell 控制台运行,输出是否正确?
  • @lit 嗯,很有趣。 1) cmd.exe2) PowerShell 控制台中的显示是相同的 - 好的。但是tasklist &gt; c:\\tasklist1.txt 在第一种情况下(没有激活chcp 1251)有损坏的符号,在第二种情况下 - 一切正常。
  • @lit 我找到了解决方案 \ (_) /

标签: r cmd locale cyrillic system2


【解决方案1】:

我找到了解决办法。

#/c - Carries out the command specified by string and then stops.
command <- function(command, 
           intern = TRUE, 
           wait = FALSE)
system(paste("cmd.exe /c", command), 
           intern = T, 
           wait = wait)

#changing our charset
command("chcp 1251")
[1] "’ҐЄгй п Є®¤®ў п бва ­Ёж : 1251" //say bye-bye to mojibake)

# and voila!
command("tasklist")
[1] ""                                                                            
[2] "Имя образа                     PID Имя сессии          № сеанса       Память"
[3] "========================= ======== ================ =========== ============"
[4] "System Idle Process              0 Services                   0        24 КБ"
[5] "System                           4 Services                   0       580 КБ"
[6] "smss.exe                       380 Services                   0     1 232 КБ"
***

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 2011-05-31
    • 2018-02-19
    • 2020-10-12
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 2015-05-19
    相关资源
    最近更新 更多