0
$\begingroup$

This is my runtime environment

$Version
$CharacterEncoding

"11.1.1 for Microsoft Windows (64-bit) (May 16, 2017)"

"CP936"

When I run

SetDirectory["C:/Windows/AppPatch"];
RunProcess["cmd", "StandardOutput", "dir\n"]

I get a such output

I know the wrong character is Chinese character. How to show it correctlly?

$\endgroup$
3
$\begingroup$

First method:

run[cmdLine_] := Module[{out}, 
                RunProcess[{$SystemShell, "/c", cmdLine <> ">test.txt"}]["StandardOutput"];
                out = Import["test.txt", CharacterEncoding -> "CP936"];
                DeleteFile["test.txt"]; out]

run["echo 你好"]

"你好"

run["dir"]

enter image description here

Second method:

Import["!echo hello 你好", "Text", CharacterEncoding -> "CP936"]

"hello 你好"

Import["!dir\n", "Text", CharacterEncoding -> "CP936"]
(*also works*)
| improve this answer | |
$\endgroup$
0
$\begingroup$

More cleaner. Just make a note here.

ImportString[
 RunProcess[$SystemShell, "StandardOutput", "dir\n"], "Text", 
 CharacterEncoding -> "CP936"]
| improve this answer | |
$\endgroup$
  • $\begingroup$ It isn't clear for what purpose you use FromCharacterCode here: ImportString already returns a grid of strings. It would be more correct to specify "Text" as second argument to ImportString. $\endgroup$ – Alexey Popkov Oct 30 '17 at 8:23
  • $\begingroup$ @AlexeyPopkov Thanks very much. It works indeed..And I have updated it just now. $\endgroup$ – yode Oct 30 '17 at 8:28
  • $\begingroup$ @AlexeyPopkov Another thing about it, Since ImportString // Options don't include option CharacterEncoding, but we can use it. It is a bug of ImportString? $\endgroup$ – yode Oct 30 '17 at 8:30
  • $\begingroup$ The CharacterEncoding option is listed on the Docs page for the "Text" import format, there is no bug of course. $\endgroup$ – Alexey Popkov Oct 30 '17 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.