본문 바로가기

python

(197)
Python Learn the basics Quiz 28 Q>배틀그라운드를 800시간을 한 유저가 있다.이 사람은 몇일 몇시간을 했는지 작성하는 프로그램을 만드시오. A>hours = 800 a = divmod(hours, 24) b = '800시간을 배틀그라운드를 한 유저는 {0}일 {1}시간을 한 것입니다.' print(b.format(a[0],a[1])) O>800시간을 배틀그라운드를 한 유저는 33일 8시간을 한 것입니다. Process finished with exit code 0
Python Learn the basics Quiz 27 Q>사격형의 면적과 둘레 길이를 구하는 클래스를 작성하시오. A>class Square: width = 0 height = 0 def __init__(self, width, height): self.width = width self.height = height def getArea(self): return self.width * self.height def getRound(self): return self.width * 2 + self.height * 2 O> Process finished with exit code 0
Python Openpyxl - write(excel file) 1. Python Openpyxl파이썬에 엑셀처리 패키지.특이점은 엑셀이 시스템에 설치가 되어있지 않아도 사용이 가능하다. 2. Openpyxl 설치pip install openpyxl 3. openpyxl 모듈 임포트import openpyxl 4. openpyxl 모듈 임포트 2from openpyxl import Workbook 5. openpyxl 기본 사용법from openpyxl import Workbook wb = Workbook() dest_filename = '파일명.xlsx' ws = wb.active ws.title = '시트명' ws['A1'] = 'A1 입력할 문자' wb.save(filename = dest_filename) 6. 코드 정리6-1. 작업할 워크북 생성wb = Wo..
Python Pickling / Unpickling 1. Python Pickle파이썬 객체(파일)을 파일에 저장 하는 과정리스트나 클랙스 같은 텍스트가 아닌 자료형을 파일로 저장하기 위하여 사용 1-1. Pickle 모듈 임포트import pickle 1-2. Pickle 모듈 기본 사용법with open('파일명', '파일모드') as file: pickle.dump(데이터 변수, file)Pickle의 파일모드는 바이트형식으로 작성하므로 wb를 사용한다. 여기서 b는 바이너리를 의미한다. 보통 확장자는 bin / dmp를 사용한다. 물론 다른 확장자를 사용해도 무관하다. 1-3. Pickle 예제 파일>import pickle dir = 'c:/windows/dir_pro' ver = '2.0' file_load = 'c:/program/en/en...
Python Open - writelines / readlines / readline 1. writelines()Code 기본 사용법>리스트변수 = ['리스트'] with open('파일명', 'w', encoding='인코딩 방식') as file: file.writelines(리스트변수) Code 예제>list_str = ['Life is too short\n','You need Python\n'] with open('python.txt', 'w', encoding='utf-8') as file: file.writelines(list_str) Code 예제 출력물> writelines는 리스트의 있는 문자열을 파일에 작성할때 사용 하는 명령어이다.주의사항은 리스트 각 문자열 끝에 \n 개행 문자를 지정 해야된다. 2. readlines()Code 기본 사용법>with open('파일명..
Python Open - With as Open with as 기본 사용법>with open('파일명', 'r', encoding='인코딩 방식') as file: file = file.read() print(file) 1. Open with as파일을 읽고 쓰고 할 때마다 매번 아래와 같은 명령어를 사용했다.file.close()하지만 이 문구를 사용하지 않고 자동으로 할 수 있는 것이 open에 with as 함수이다. 1-1. Code>with open('i love python.txt', 'r', encoding='utf-8') as file: file = file.read() print(file) 1-1 Code 출력물>Hello, world!Welcome to Python!!Hello Process finished with exit..
Python Open - w / r / a Open 기본 사용법>file = open('파일명', '파일모드', encoding='인코딩 방식') file.write('문자열') file.close()Python 에서 가장 기본 축이 될 수 있는 Open 함수 입니다.파일에 문자열을 쓰는 가장 기본적인 Code 입니다.파일이름은 데이터의 위치를 지정할 수도 있고 기본적으로는 코드가 저장 되어있는 폴더 안에 파일이 생성됩니다.인코딩은 파일을 만들 때 해당 인코딩 방식으로 만들때 사용한다. ecu-kr 한국어 지원 utf-8 다국어 지원 1. w(write)file = open('i love python.txt', 'w', encoding='utf-8') file.write('Hello, world!\n') file.write('W..
Pycharm 한글 에러 발생 해결 방안 Issue>1. Pycharm에서 한글로 출력시 에러가 발생 하거나 글씨가 깨지는 현상이 벌어짐2. 한글 인코딩 문제 발생 Solutions>1. Pycharm Settuings 변경 방법1-1. File > Settings 1-2. Editor > File Encodings > project Encoding > Default encoding for properties fileUTF-8 로 변경 후 Pycharm 종료 후 재시작 2. 코드창 윗부분에 아래 해당 주석 추가#-*- coding: utf-8 -*-