0
#!/usr/bin/python3

import argparse

vokaler = "aouåeiyäöAOUÅEIYÄÖ"
konsonanter = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"

def viskspraket(text):
    """Removes all vowels from the sentence"""
    return "".join([x if x not in vokaler else "" for x in text])

def rovarspraket(text):
    """Consonants are doubled and "o" is put inbetween them"""
    return "".join([x + "o" + x if x in konsonanter else x for x in text])

parser = argparse.ArgumentParser()
parser.add_argument("-r", help="rovarspraket", action="store_true")
parser.add_argument("-v", help="viskspraket", action="store_true")
args = parser.parse_args()
textFromTerminal = input()

print(textFromTerminal)

if args.r:
    print(rovarspraket(textFromTerminal))

elif args.v:
    print(viskspraket(textFromTerminal))

This is the python script that I've written and I need to be able to translate a already translated string from bash. This is what I'm doing currently to translate a string:

$ echo "random string"|./translation.py -r

And the output is then:

roranondodomom sostotrorinongog

Now, if I wanted to translate "random string" first to rovarspraket and then to viskspraket. So "roranondodomom sostotrorinongog" is what I now want to translate. Is there a way to do this without copying and pasting and just repeating the same prodeduce but with "-v" as the command parameter instead of "-r".

I tried double piping:

$ echo "random string"|./translation.py -r |./translation.py -v

But that seems to simply igonore the first pipe?

0

Your problem is that you're using input(). From the Python 3 official docs: "The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that."

It's only returning the first line from stdout. Since you output the input string as the first line of standard out with your script, the second script is only reading that.

Here's a fix using sys.stdin. You could also fix this with multiple calls to input().

import sys                                                                                                                             
with  sys.stdin as stdin_file:
    textFromTerminal=stdin_file.read()
    print(textFromTerminal)

    if args.r:
        print(rovarspraket(textFromTerminal))

    elif args.v:
        print(viskspraket(textFromTerminal))

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.