Python
HTML
borgWarp
sourceReading
highlighting

Python3 > colorText_180707.py > v0.1 > optという文字に色付けした結果を出力

動作環境
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の復習もかねて実装。

code v0.1

colorText_180707.py
import re


'''
v0.1 Jul. 07, 2018
  - add color html tag for a fixed text ('opt')
'''

# on Python 3.5.2

SRC_FILE = 'in_180707.txt'
COL_TEXT = 'opt'
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='')

使用例

in_180707.txt
This opt is used as a option.
opt.list=1
suboption is also specified so that
$ python3 colorText_180707.py 
This <font color=red>opt</font> is used as a <font color=red>opt</font>ion.
<font color=red>opt</font>.list=1
sub<font color=red>opt</font>ion is also specified so that

標準出力を貼り付けたのが以下。
This opt is used as a option.
opt.list=1
suboption is also specified so that

TODO

  • 色付けする文字列を実行時引数で指定できるように