0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

manなど空白と改行が邪魔な英文をGoogle翻訳に投げるPythonスクリプト

Last updated at Posted at 2020-06-29

manとかRFCとかconfといったテキストベースの情報をGoogle翻訳にかけたいけど、空白とか#文字によるコメントアウトや改行が邪魔で変な感じになりますよね。私は、こんな感じでやってます。

まず、Pythonでクリップボードの中身を取得し、空白やら改行を除去します。

#!/usr/bin/env python

import pyperclip
s = pyperclip.paste()

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

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

print(s)
pyperclip.copy(s)

これを起動して、Google翻訳のコマンドライン版であるtransにパイプでつなげると、あら不思議、きれいに翻訳されてるじゃあーりませんか。

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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?