0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQLのin句の作成(キーに','を付ける)

Last updated at Posted at 2021-04-18

SQLのin句の作成用

  • 自分用
  • 目的:データ調査時の主キーでSQL作成する際、in句の作成時間短縮
  • 今まで:エディタのキーボードマクロ
  • これから:対象をクリップボードにコピーしてからpythonプログラムを実行
  • 実行するとどうなる:

■実行前
aaa
bbb
ccc
■実行後
in (
'aaa'
,'bbb'
,'ccc'
)

前提

pyperclipがインストールされていること
pip install pyperclip

コード

#! python3
import pyperclip
text = pyperclip.paste()
lines = text.split('\r\n')

for i in range(len(lines)):
    if i == 0:
        lines[i] = ' \'' + lines[i] + '\''
    else:
        lines[i] = ',\'' + lines[i] + '\''
text = 'in (\r\n'
text = text + '\r\n'.join(lines)
text = text +'\r\n)'

pyperclip.copy(text)
0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?