Add quotation at the start and end of each line

From LemonWiki共筆
Revision as of 14:55, 20 February 2022 by Unknown user (talk) (→‎Add quotation at the start and end of each line)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

將每行的文字,都加上引號框起來,並且移除換行

// before
Elmo
Emie
Granny Bird

// after
'Elmo', 'Emie', 'Granny Bird'

Add quotation at the start and end of each line[edit]

Using Notepad++[1] or sublime text

  • Find what: (.*)
  • Replace with: '\1',
    (如果要使用雙引號框起來,則是 Replace with: "\1")

Using python

import re

input = "a\nbb  \nccc\n \ndddd\n"
print(input)

pattern = r"(.*)\n"
replace = r'"\1"\n'
print("#### result ####\n")
print(re.sub(pattern, replace, input))

Add quotation at the start and end of each line, remove return symbol & add comma symbol[edit]

Data condition 1: Trim whitespace of each line[edit]

方法1: 使用 Sublime TextNotepad++EmEditor。該方法有處理每行的前面或後面可能有一格或多格空白

如果使用 Mac icon_os_mac.png 作業系統

  • Find what: (\S+)(\s?)+$\n
  • Replace with: '\1',
    (如果要使用雙引號框起來,則是 Replace with: "\1", )

如果使用 Win Os windows.png 作業系統,需要修改換行符號 \n\r\n

  • Find what: (\S+)(\s?)+$\r\n on Mac icon_os_mac.png
  • Replace with: '\1',
    (如果要使用雙引號框起來,則是 Replace with: "\1", )

Data condition 2: Not trim whitespace of each line[edit]

方法2: 使用 Sublime Text 或 EmEditor Icon_exclaim.gif 該方法沒有處理每行的後面可能有一格或多格空白

  • Find what: (.*)$\n(\S+)$\n(\S+)\n
  • Replace with: '\1',


References[edit]