LoginSignup
1
1

More than 5 years have passed since last update.

MemoryEnhancer | Python > string > banner用の文字列として10文字ずつ二重引用符で区切る | テキストにダブルクオートがあった場合のエスケープ処理

Last updated at Posted at 2018-01-18
動作環境
CentOS 6.8 (64bit)
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 

処理概要

bannerコマンド用に文字列を適度な長さに二重引用符で区切る。

code v0.1

split_string_180118.py
# on Python 2.6.6
# coding rule:PEP8

txt = "They are put in [as needed] and taken out after they are used."


def split_string(intxt, len):
    res = ""
    for idx, elem in enumerate(intxt):
        if idx % len == 0:
            res += '"'  # starting
        res += elem
        if idx % len == (len-1):
            res += '" '  # ending
    return res

res = split_string(txt, len=10)
print(res)

run
$ python2 split_string_180118.py 
"They are p" "ut in [as " "needed] an" "d taken ou" "t after th" "ey are use" "d.

code v0.2

最後の"d.の閉じ忘れがあった。
not inとslicing?で対応した。

if not '"' in res[-1]:
    res += '"'

split_string_180118.py
# on Python 2.6.6
# coding rule:PEP8

txt = "They are put in [as needed] and taken out after they are used."


def split_string(intxt, len):
    res = ""
    for idx, elem in enumerate(intxt):
        if idx % len == 0:
            res += '"'  # starting
        res += elem
        if idx % len == (len-1):
            res += '" '  # ending
    return res

res = split_string(txt, len=10)
if not '"' in res[-1]:
    res += '"'
print(res)

run
$ python2 split_string_180118.py 
"They are p" "ut in [as " "needed] an" "d taken ou" "t after th" "ey are use" "d."

banner実行してみた

$ banner "They are p" "ut in [as " "needed] an" "d taken ou" "t after th" "ey are use" "d."

#######  #     #  #######  #     #           #     ######   #######        
   #     #     #  #         #   #           # #    #     #  #              
   #     #     #  #          # #           #   #   #     #  #              
   #     #######  #####       #           #     #  ######   #####          
   #     #     #  #           #           #######  #   #    #              
   #     #     #  #           #           #     #  #    #   #              
   #     #     #  #######     #           #     #  #     #  #######        


#     #  #######        ###  #     #        #####     #      #####         
#     #     #            #   ##    #        #        # #    #     #        
#     #     #            #   # #   #        #       #   #   #              
#     #     #            #   #  #  #        #      #     #   #####         
#     #     #            #   #   # #        #      #######        #        
#     #     #            #   #    ##        #      #     #  #     #        
 #####      #           ###  #     #        #####  #     #   #####         


#     #  #######  #######  ######   #######  ######   #####           #     
##    #  #        #        #     #  #        #     #      #          # #    
# #   #  #        #        #     #  #        #     #      #         #   #   
#  #  #  #####    #####    #     #  #####    #     #      #        #     #  
#   # #  #        #        #     #  #        #     #      #        #######  
#    ##  #        #        #     #  #        #     #      #        #     #  
#     #  #######  #######  ######   #######  ######   #####        #     #  


######         #######     #     #    #  #######  #     #        #######  
#     #           #       # #    #   #   #        ##    #        #     #  
#     #           #      #   #   #  #    #        # #   #        #     #  
#     #           #     #     #  ###     #####    #  #  #        #     #  
#     #           #     #######  #  #    #        #   # #        #     #  
#     #           #     #     #  #   #   #        #    ##        #     #  
######            #     #     #  #    #  #######  #     #        #######  


#######           #     #######  #######  #######  ######         #######  
   #             # #    #           #     #        #     #           #     
   #            #   #   #           #     #        #     #           #     
   #           #     #  #####       #     #####    ######            #     
   #           #######  #           #     #        #   #             #     
   #           #     #  #           #     #        #    #            #     
   #           #     #  #           #     #######  #     #           #     


#######  #     #           #     ######   #######        #     #   #####   
#         #   #           # #    #     #  #              #     #  #     #  
#          # #           #   #   #     #  #              #     #  #        
#####       #           #     #  ######   #####          #     #   #####   
#           #           #######  #   #    #              #     #        #  
#           #           #     #  #    #   #              #     #  #     #  
#######     #           #     #  #     #  #######         #####    #####   


######          
#     #         
#     #         
#     #         
#     #   ###   
#     #   ###   
######    ###   

9文字までかもしれない。
CentOSでは9文字/行の表示だった。

CentOSとUbuntuとRaspbianでbannerの横文字長さが変わるのかもしれない。
ターミナルのサイズにもよるのだろうか。

教えていただいた事項

@shiracamus さんのコメント
にてテキストにダブルクオートがあった場合のエスケープ処理のコードを教えていただきました。
また処理ではjoin()やmap()やreplace()などを使われておられます。

情報感謝です。

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