[Python] Google Sheets 읽기

실험 환경 OS: Ubuntu Linux 18.04 Python Version: 3.7 gspread, oauth2client 설치 필요 $pip install gspread oauth2client 인증 과정 먼저 API를 사용하기 위해서 구글 개발자 콘솔에 접속해서 몇 가지 설정을 진행해야 한다. 아래 사이트에 접속한 다음에 그림에 따라서 인증 과정을 진행하면 된다. Web: https://console.developers.google.com/ 그림 2 Step 3의 Project Name은 원하는 이름을 선택하면 된다. 그림 … Read more

[Python] argparse 사용 예제

Python으로 작성한 코드를 실행할 때 특정 Argument를 추가해야만 실행이 가능하도록 코드를 작성하고 싶은 경우가 발생한다. Python에서 “argparse”를 사용하면 쉽게 Argument를 추가할 수 있다. Argparse 사용하기 아래 코드1은 Argparse를 사용하는 예제 코드이다. argparse.ArgumentParser(…): argparse를 사용하기 위해서 가장 먼저 해당 코드를 사용해서 Variable을 생성한다. VARIABLE.add_argument(…): 생성된 Variable에 add_argument(..)를 사용해서 Argument 옵션을 하나씩 추가한다. 위 2가지가 Argparse를 사용하는 … Read more

[Python] namedtuple (네임드튜플) 사용 (Struct like C)

namedtuple은 C언어 Struct와 같이 Value를 여러 개 저장해서 Variable 이름으로 접근이 가능하다. 개인적으로 C Struct와 비슷해서 사용하게 되었다. 출처 1에 따르면 namedtuple은 Python 기본자료형이 아니라고 한다. namedtuple을 사용하기 위해서는 아래와 같이 Collections를 추가해야 한다. from collections import namedtuple namedtuple 사용하기 출처 1에 제공하는 예제를 조금 변경하여서 아래 예제 코드를 하나 작성하였다. from collections import namedtuple … Read more

[Python] “cURL” CMD를 Python Request 모듈에서 로딩하기

“client URL (cURL: curl)은 다양한 통신 프로토콜을 이용하여 데이터를 전송하기 위한 라이브러리와 명령 줄 도구를 제공하는 컴퓨터 소프트웨어 프로젝트이다” (출처 1). 간단히 설명하면 Linux Terminal 등에서 데이터를 전송하기 위해서 사용된다는 의미이다. curl을 사용하여 특정 사이트에서 정보를 가져오는 작업을 Python을 사용하여 자동화하려고 하였다. 그 과정에서 curl CMD를 Python Requests 모듈이 사용할 수 있도록 변경하는 작업이 필요하다. curl … Read more

[Python] 주식 종목 코드 및 가격 로딩 (Loading Stock Price and Code Number using Requests Module)

목적 네이버와 한국증권금융 홈페이지에서 주식 종목 코드 및 주식 종목 가격 읽기 Request 모듈 사용 방법을 알고 싶어서 만들어본 예제 코드 환경: Ubuntu 16.04 + Python3 Prerequisite Module(s): reqeusts, bs4, BeautifulSoup import requests from bs4 import BeautifulSoup #MK: 한국 주식 가격 및 주식 종목 번호 검색 class mkGetStockPrice: def __init__(self): self.__webAddr = “https://finance.naver.com/item/main.nhn?code=” self.__codeAddr = … Read more

[Python] Gmail SMTP 서버 사용해서 이메일 보내기 (Sending Email using Gamil ID)

목적: Gmail SMTP 서버를 사용하여 이메일 발송하기 환경: Ubuntu 16.04 + Python 3 Prerequisite Module(s): smtplib import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText class mkSendEmail: def __init__(self, sentTo, subject, content): emailAddr = “sender@mail.com” #MK: sender email address emailPass = “*********” #MK: password for sender email smtpAddr = “smtp.mail.com” #MK: smtp server. in case of … Read more

[Python] Bash/Powershell Command 실행 (Bash/Powershell CMD)

목적: Linux Bash CMD나 Window PowerShell CMD를 Python에서 실행하는 방법 환경: Widow 10 + Python 3.6 import os cmd = “adb shell input keyevent 224” os.system(cmd) //MK: 단순히 cmd를 실행하여 결과를 출력함 tmp = os.popen(cmd).read() //MK: cmd를 실행한 결과가 tmp에 저장됨 설명 os.system()의 경우 실행된 결과가 창으로 출력됨. 위 예제의 경우 출력되는 결과가 없어서 출력되는 내용이 없음 … Read more

[Python] GUI와 Multi-Threading 사용 (GUI + Multi-Threading)

목적: GUI (tkinter)와 Multi-Threading (threading) 사용 환경: Linux (Ubuntu 16.04) Python Version: 3.5.2 import queue #MK: queue를 사용하기 위해 추가 import threading #MK: threading을 사용하기 위해 추가 import time #MK: time을 사용하기 위해 추가 import tkinter #MK: tkinter(GUI)를 사용하기 위해 추가 class mainThread: def __init__(self): self.tk = tkinter.Tk(); def beginThread(self): self.queue = queue.Queue(); #MK: queue를 … Read more

[Python] GUI 폴더 위치 선택 (GUI asking directory path)

목적: GUI화면에서 특정 폴더 위치를 선택하여 해당 path 값을 알기 위해 사용 환경: Linux 3.10 (Centos) Python Version: 3.4 from tkinter import filedialog from tkinter import * root = Tk() root.dirName=filedialog.askdirectory(); print (root.dirName); 설치 yum install python34-tkinter 설명 filedialog.askdirectory()함수를 사용하여 선택한 directory path값을 return으로 받을 수 있음 filedialog.askopenfile(initialdir=’path’, title=’select file’, filetypes=((‘jpeg files’, ‘*.jgp’), (‘all files’, … Read more

[Python] csv 파일 쓰기 (csv file writing)

목적: 특정 데이트 값은 csv 파일에 작성하기 위한 방법 환경: Linux 3.10 (Centos) Python Version: 3.6 #Writing data on csv file import csv f = open(‘output.csv’, ‘w’, encoding=’utf-8′, newline=”) wr = csv.writer(f) wr.writerow([1, ‘mkblog’]) wr.writerow([2, ‘co’]) wr.writerow([3, ‘kr’]) f.close() 설명 open 함수를 사용하여 파일을 생성함 csv.writer()와 writerow()를 사용하여 csv 파일에 데이터 값 작성을 진행함 writerow()의 함수 parameter에 list[]값을 … Read more