LoginSignup
1
1

More than 5 years have passed since last update.

Python > ||| が続ている時に2行目に|:-:|:-:|を入れる

Last updated at Posted at 2018-04-25

関連

処理概要

はてな記法のテーブル表記をMarkdown記法にする。

| 1 | 2 |
|   |   |
|   |   |
テスト
| 1 | 2 |
|   |   |
|   |   |
| 1 | 2 |
|:-:|:-:|
|   |   |
|   |   |
テスト
| 1 | 2 |
|:-:|:-:|
|   |   |
|   |   |

code v0.1

import sys

# on Python 3.5

# read from stdin
lines = sys.stdin.readlines()

def getTableFormat(numVertical):
    wrk = "|"
    for loop in range(numVertical - 1):   # -1: except for first
       wrk += ":-:|"
    return wrk

count = 0
for elem in lines:
    wrk = elem
    if "|" in wrk:
       count += 1
       if count == 2:
           num = wrk.count('|')
           fmt = getTableFormat(num)
           print(fmt)
    else:
       count = 0
    print(elem, end='')

code v0.2

import sys

# on Python 3.5

'''
v0.1 Apr. 24, 2018
   - sub list is converted to Markdown style
'''

# read from stdin
lines = sys.stdin.readlines()


def getTableFormat(numVertical):
    wrk = "|"
    for loop in range(numVertical - 1):   # -1: except for first
       wrk += ":-:|"
    return wrk


def conv_tableFormat(astr):
    global countTbl
    if "|" in astr:
        countTbl += 1
        if countTbl == 2:
            num = astr.count('|')
            fmt = getTableFormat(num)
            return fmt + "\r\n" + astr
    else:
        countTbl = 0
    return astr

countTbl = 0  # Table format counter
for elem in lines:
    wrk = conv_tableFormat(elem)
    print(wrk, end='')

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