본문 바로가기

수업정리/Fundamental

(22)
[Python] 한글 유니코드 12 - 기말 sqlite3 을 연결해서 쿼리를 실행하고 그 결과를 .txt 파일로 저장하는 방법 https://greenbnn98.tistory.com/88 run_sql.sh : 커멘드에서 쿼리를 실행해 결과를 얻을 수 있다 han_sqldb.py 에서 쿼리를 실행해 결과를 얻을 수 있다 test_sqldb.py : sqlite3 딥비를 연결하고 원하는 쿼리문 실행하는 함수로 결과 뽑아내기 https://greenbnn98.tistory.com/89 엑셀 파일을 다운로드 해서 데이터를 읽기 구글 드라이브에서 파일을 읽어오기 pandas_xlsx_func.py : 판다를 이용해 엑셀 파일 데이터를 읽는 함수 작성 https://greenbnn98.tistory.com/90 music_pandas_xls2sql.py ..
[Python] 한글 유니코드 11 - sqlite3 wiki.sqlite3 create table url_body (url text not null, body blob, date text not null, primary key(url)); alter table wiki_links add column fk_rul references url_body(url); scrape_wiki_random_blob.py #code:utf-8 from crawl_wiki2 import find_first_link, continue_crawl #main import time import urllib.parse # to decode any URL string from datetime import datetime from wiki_sqldb import conne..
[Python] 한글 유니코드 10 - crawl_wiki2.py import time import urllib import bs4 import requests start_url = "https://ko.wikipedia.org/wiki/Special:Random" target_url = "https://ko.wikipedia.org/wiki/Philosophy" def find_first_link(url): response = requests.get(url) html = response.text soup = bs4.BeautifulSoup(html, "html.parser") body_text = soup.get_text() # This div contains the article's body # (June 2017 Note: Body ne..
[Python] 한글 유니코드 9 - urllib.py, scrape_wiki_random.py, 위키에서 읽고 데이터 받기? wget http://www.py4e.com/code3/socket1.py : 소켓은 통신을 위한 객체로 데이터를 주고 받을 수 있다. 예제 파일 다운 python socket1.py : 예제 파일 실행 wget https://www.py4e.com/code3/urllib1.py : urllib1.py 파일은 urllib 라이브러리를 사용하여 웹 페이지를 가져오고 출력하는 예제 코드입니다. urllib 모듈을 사용하여 웹 요청을 수행하고, 서버에서 받은 응답을 처리하여 웹 페이지의 내용을 출력할 것입니다. python urllib1.py : 예제 파일 실행 urllib2.py import urllib.request fhand = urllib.request.urlopen('http://data.pr4e.o..
[Python] 한글 유니코드 9 - 질의처리(select, from, where), 쿼리문 파이썬 파일만들기 select * from “Kpop_release>May”; 5월 테이블에서 모든 음악 select Album, “Artist(s)” from “Kpop_release>May” ; 5월 테이블에서 Album, Artist(s) 속성만 select count(*) from “Kpop_release>May” ; 5월 테이블 모든 음악 개수 select * from “Kpop_release>May” group by "Genre(s)"; 5월 테이블 모든 음악 장르 그룹별로 보여줘 select Album from “Kpop_release>May” where “Genre(s)” = “Dance” ; 5월 테이블에서 댄스 엘범만 select Album from “Kpop_release>May” where “Arti..
[Python] 한글 유니코드 8 - pandas, read_excel(), to_sql() pandas 를 이용해서 xlst 을 sqldb 로 전달하기 music_pandas_xls2sql.py #coding : utf-8 import pandas as pd import sqlite3 import sys def connect_db(dbname): conn = sqlite3.connect(dbname) return conn def insert_with_pd (conn, infilename): try: df = pd.read_excel (infilename) from pathlib import Path tablename = Path (infilename).stem # 'music/Albums.xlsx' -> Albums df.to_sql(tablename, conn) # this does 'cre..
[Python] 한글 유니코드 7 - sqlite3, index, gdown 파일 연결하기, pandas, openpyxl sqlite3 hangulDB.sqlite3 create index index_char_freq on han_characters(freq); 인덱스 만들어주기 > 나중에 쿼리 실행 빨라짐 .output t.sql .dump .quit grep -n 'INDEX' t.sql t.sql 파일에 있는 인덱스 출력? >1413:CREATE INDEX index_char_freq on han_characters(freq); 우리는 test_sqldb.py 파일에서 han_sqldb.py 를 import 해서 db 에 연결하고 여러 쿼리문을 sqlite3 에서 실행할 수 있는 함수들을 사용했다. 이제 추가로 index 를 부여하는 방법을 보고 얼마나 빠른지 보자 cp test_sqldb.py test_index_s..
[Python] 한글 유니코드 6 - sqlite3, test_sqldb.py 참고 사이트 : Command Line Shell For SQLite sqlite3 hangulDB.sqlite3 : 우리의 DB 파일에 연결 .table : table 들 출력 .output hangulDB.sql : output filename 지정 .dump / .exit : 종료 파일 백업 sqlite3 hangulDB.test : 새로운 데이터 파일 만들고 연결 ( 파일이 없다면 ) .read hangulDB.sql : 한글 DB 불러오기 select * from han_characters; 테이블 안에 있는 모든 튜플 출력 select character, freq from han_characters; 테이블에 있는 charater, freq 에 해당하는 튜플 출력 select * from ha..