Python 2> Implementation> {Erase leading whitespace | Add Markdown header (###)} | Example of sed erasing file contents

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

Related: GAMESS> Output file items

specification

Suppose there are multiple spaces before a particular character.

$ cat sample.in 
   A B C
  D E F
G    H I

I want to convert the above format to:

$ cat sample.md 
###A B C
###D E F
###G    H I

code

toMarkdown_170829.py


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import subprocess
import sys

# On Python 2.6.6

# coding rule:PEP8

# { Configuration----
# Set strings' lists for GAMESS output
targets = ("A B C",
           "D E F",
           "G    H I"
           )
# } Configuration----

if len(sys.argv) < 3:
    print('ERROR: invalid runtime parameter')
    print('\ntype:')
    print('python %s [input file] [output file]' % sys.argv[0])
    sys.exit()

IN_FILE = sys.argv[1]
OUT_FILE = sys.argv[2]
WRK_FILE = 'wrk.md'

for elem in targets:
    # 1. replace
    cmd = "sed 's/ *%s/###%s/g' %s > %s" % (
        elem, elem, IN_FILE, WRK_FILE)
    # print(cmd)  # for debug
    subprocess.call(cmd, shell=True)
    # 2. move
    # because (sed 's/A/B/g' in > in) will delete [in] file
    cmd = 'mv %s %s' % (WRK_FILE, OUT_FILE)
    subprocess.call(cmd, shell=True)

    # sys.exit()  # for debug
    IN_FILE = OUT_FILE

run


$ python toMarkdown_170829.py sample.in sample.md

What I noticed with sed

When you execute sed's / A / B / g'out.md> out.md, the contents of out.md disappear. Therefore, the above implementation uses a temporary file [WRK_FILE].

reference

-"2. Quantum specifier" in Regular expression cheat sheet for forgetful people -Python: Get command line arguments – sys.argv variable @ Yukun's Blog

Recommended Posts

Python 2> Implementation> {Erase leading whitespace | Add Markdown header (###)} | Example of sed erasing file contents
Easy encryption of file contents (Python)
Template of python script to read the contents of the file