python成語代碼?
import sqlite3
import argparse
def main():
#添加參數
parser = argparse.ArgumentParser()
parser.add_argument('word', help='要查詢的成語')
parser.add_argument('-blur', action='store_true', help='開啟模糊查詢')
parser.add_argument('-detail', action='store_true', help='開啟顯示成語詳細信息')
#解析輸入的參數
args=parser.parse_args()
word=args.word
isblur=args.blur
isShowDetail=args.detail
con=sqlite3.connect('cy/cy.db')
cursor=con.cursor()
fields=['word']
if isShowDetail:
fields.extend(['spell','paraphrase','source','example'])
strFields=','.join(fields)
sql=f'select {strFields} from cy '
#類似c語言的iif
strwhere=f" where word like '%{word}%'" if isblur else f" where word='{word}' "
sql =sql + strwhere
rows=cursor.execute(sql).fetchall()
for row in rows:
print(row[0] )
if isShowDetail:
print(f"拼音:{row[1]}")
print(f"釋義:{row[2]}")
print(f"出處:{row[3]}")
print(f"示例:{row[4]}")
if __name__=='__main__':
main()