Python script that throws English sentences such as man that are disturbing by spaces and line breaks to Google Translate

I want to apply man, RFC, or text-based information to Google Translate, but spaces and line breaks are annoying and strange. I'm doing like this.

First, get the contents of the clipboard with Python and remove whitespace and line breaks.

#!/usr/bin/env python

import pyperclip
s = pyperclip.paste()

s = s.replace("\r"," ")
s = s.replace("\n"," ")

while -1 != s.find("  "):
    s = s.replace("  "," ")

print(s)
pyperclip.copy(s)

If you start this and connect it to trans, which is the command line version of Google Translate, with a pipe, it will be strangely translated beautifully.

python ~/python/text_remove_crlf.py | trans {en=ja} -b

Recommended Posts

Python script that throws English sentences such as man that are disturbing by spaces and line breaks to Google Translate
Read English sentences by hitting Google Translate API with Python without using the distributed module