Work with 'echo' command line in Python code

28,498

Solution 1

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).

Solution 2

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)
Share:
28,498
rakuscientist
Author by

rakuscientist

Updated on November 26, 2021

Comments

  • rakuscientist
    rakuscientist over 2 years

    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.

  • rakuscientist
    rakuscientist about 7 years
    Thanks. It is helpful.