rakuscientist
rakuscientist

Reputation: 53

Work with 'echo' command line in Python code

I tried to use Python with command line parser 'kakasi' together. Here is my code:

# -*- coding: utf-8 -*-
import os
text = 'スッキリわかる Java入門 実践編 第2版'
cmd = "echo $text | iconv -f utf8 -t eucjp | kakasi -i euc -w | kakasi -i euc -Ha -Ka -Ja -Ea -ka"
os.system(cmd)
-------------------

The result is empty line as below:

Process finished with exit code 0

Actually, the result should be like this:

sukkiri wakaru Java nyuumon jissenhen dai 2 han

I need a help.
Thanks in advance.

Upvotes: 2

Views: 49095

Answers (2)

hiro protagonist
hiro protagonist

Reputation: 46849

text is a variable known to the python interpreter only; the shell you execute your command in has no idea what $text is (it will evaluate to an empty string).

you could try this:

cmd = "echo '{}' | iconv ...".format(text)

that way you get your string to the command you want to execute. (can't test if there are other things that do not work...)

(side note: i like the sh module for this kind of thing. it is not in the standard library though).

Upvotes: 4

Surajano
Surajano

Reputation: 2688

You should need to use String formatting.

SOLUTION :

# -*- coding: utf-8 -*-
import os
text = 'スッキリわかる Java入門 実践編 第2版'
cmd = "echo {} | iconv -f utf8 -t eucjp | kakasi -i euc -w | kakasi -i euc -Ha -Ka -Ja -Ea -ka".format(text)
os.system(cmd)

Upvotes: 3

Related Questions