1
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 5 years have passed since last update.

Python3 > colorText_180707.py > v0.2 > 実行時引数によるキーワード指定 (sys.argvの使用) || v0.3 > 改行用に<BR>追加 || v0.4 color指定修正

Last updated at Posted at 2018-07-08
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0
GNU grep 2.25

関連

処理概要

  • テキストをファイルから読込む
  • htmlタグで特定の文字に色付けして標準出力
  • (今回) 実行時引数で色付けキーワードの指定

Pythonの復習もかねて実装。

参考

v0.2

code v0.2

colorText_180707.py
import re
import sys

'''
v0.2 Jul. 08, 2018
  - read command-line arguments
v0.1 Jul. 07, 2018
  - add color html tag for a fixed text ('opt')
'''

# on Python 3.5.2

args = sys.argv
if len(args) < 2:
    print('Error: keyword is not specified')
    print('[cmd] [keyword]')
    sys.exit()

SRC_FILE = 'in_180707.txt'
col_text = args[1]
inRed = '<font color=red>' + col_text + '</font>'

with open(SRC_FILE) as fd:
    lines = fd.readlines()
    for aline in lines:
        wrk = aline.replace(col_text, inRed)
        print(wrk, end='')

使用例

$ python3 colorText_180707.py 
Error: keyword is not specified
[cmd] [keyword]
$ python3 colorText_180707.py used
This opt is <font color=red>used</font> as a option.
opt.list=1
suboption is also specified so that...

htmlとして閲覧すると下記となる。

This opt is used as a option.
opt.list=1
suboption is also specified so that...

Google Chromeでの表示

$ python3 colorText_180707.py used > tmp.html
$ google-chrome tmp.html 

qiita.png

改行がなくなった。
htmlと解釈されるため、<CR><LF>では改行されなくなる。

v0.3

改行用の<BR>を追加。

code v0.3

colorText_180707.py
import re
import sys

'''
v0.3 Jul. 08, 2018
  - add <BR> for each new line
v0.2 Jul. 08, 2018
  - read command-line arguments
v0.1 Jul. 07, 2018
  - add color html tag for a fixed text ('opt')
'''

# on Python 3.5.2

args = sys.argv
if len(args) < 2:
    print('Error: keyword is not specified')
    print('[cmd] [keyword]')
    sys.exit()

SRC_FILE = 'in_180707.txt'
col_text = args[1]
inRed = '<font color=red>' + col_text + '</font>'

with open(SRC_FILE) as fd:
    lines = fd.readlines()
    for aline in lines:
        wrk = aline.replace(col_text, inRed)
        print(wrk, end='<BR>')

$ python3 colorText_180707.py opt > tmp.html
$ google-chrome tmp.html 

qiita.png

Chromeで色付きで見ることができるようになった(上記実行結果ではin_180707.txtにgrep検索した結果を保存)。

v0.4

@shiracamus さんのコメントに基づき、color指定の部分を訂正しました。
情報感謝です。

colorText_180707.py
import re
import sys

'''
v0.4 Jul. 08, 2018
  - fix bug > color attribute without double quotation
v0.3 Jul. 08, 2018
  - add <BR> for each new line
v0.2 Jul. 08, 2018
  - read command-line arguments
v0.1 Jul. 07, 2018
  - add color html tag for a fixed text ('opt')
'''

# on Python 3.5.2

args = sys.argv
if len(args) < 2:
    print('Error: keyword is not specified')
    print('[cmd] [keyword]')
    sys.exit()

SRC_FILE = 'in_180707.txt'
col_text = args[1]
inRed = '<font color="red">' + col_text + '</font>'

with open(SRC_FILE) as fd:
    lines = fd.readlines()
    for aline in lines:
        wrk = aline.replace(col_text, inRed)
        print(wrk, end='<BR>')

1
1
2

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
1
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?