코딩 공부/Python 3

[Python] json, jsonl 파일 읽고 쓰기

json 파일 읽기import jsonwith open("sample_file.json", "r", encoding='UTF-8') as json_file: json_data = json.load(json_file)with open의 encoding 옵션은 없어도 되는 값encoding은 말 그대로 문자 encoding 방식default는 windows의 경우 보통 'cp1252' 또는 'mbcs' ('cp949'와 유사한 애들), mac이나 linux의 경우 'UTF-8'encoding설명utf-8국제 표준. 거의 대부분에서 사용됨utf-8-sigByte Order Mark (BOM)이 포함된 UTF-8 (엑셀/윈도우 호환성)cp949윈도우 한국어 기본 인코딩 (한글이 깨지는 원인으로 빈번하게 등..

[Python] 간단한 random 함수 기능 정리 (+ numpy.random)

random (표준 libirary)numpy.random (NumPy 모듈)importimport randomimport numpy as np속도느림 (Python 기반)빠름 (C 기반, 벡터화 되어있음)배열 지원X (단일 값만 지원)배열 단위 지원 가능통계 분포 지원기본적 지원만 가능다양한 분포 제공 (정규분포, 포아송분포, 베타분포 등)시드 설정random.seed() # 괄호 안에 seed numbernp.random.seed() #괄호 안에 seed number용도간단한 샘플링, 실험대규모 데이터 작업함수randomnumpy.random정수 랜덤randint(a, b)a~b까지 정수 하나 출력randint(a, b)a 이상 b 미만의 정수 출력부동소수 랜덤random()[0,1)의 소수 하나 출..