본문 바로가기

Python/PythonLibrary20

open( ) / with~ as~ 문 / open() >> quit() , close() 닫아주지 않으면 객체가 열려있어 메모리 점유 with~as~를 통해 자동으로 객체를 닫아줄 수 있음 # 경로에 \를 쓸 경우 \\로 두개를 쓰거나 앞에 r문자를 붙여줘야 함) #Raw String : r'~~\디렉토리~~' path = '경로' # r : 읽기모드 / w : 쓰기모드 / a : 추가모드 (파일 마지막) file = open(path='/파일명', 'w') for i in range(1,11) : data = f'{i}번째 줄\n' file.write(data) file.close() file = open(path='/파일명', 'r') # 1. read_line으로 읽어오기 line = file.readline() print(line) fi.. 2022. 10. 7.
schedule 모듈을 이용한 자동화 schedule 모듈을 이용한 파이썬 특정시간 자동 실행 import schedule # 스케쥴 모듈 import datetime # 현시간 확인 import time def test(): print('test용') def test2(text) : print(text) # 주기설정 # 1. 매개변수 없음 : schedule.every(시간).시간단위.do(함수명) # 2. 매개변수 있음 : schedule.every(시간).시간단위.do(함수명, 매개변수) schedule.every(10).seconds.do(test) # test 함수 10초마다 실행 schedule.every(10).seconds.do(test2, 'test2용 매개변수') # test2 함수 10초마다 실행 schedule.ever.. 2022. 10. 7.
win32com / xls , xlsx 파일 변환하기 import win32com.client as win32 # 엑셀 Application excel = win32com.client.Dispatch("Excel.Application") #엑셀 프로그램 실행 # 새 엑셀 만들기 wb = excel.Workbooks.Add() ws = wb.Worksheets("sheet1") # 기존 엑셀 불러오기 wb = excel.Workbooks.Open("경로/파일명.확장자") # Cell에 데이터 작성 ws.cells(1,1).Value = "text" # 1,1에 text라는 데이터 작성 ws.Range("A2").Value = "text" # 1,2에 text라는 데이터 작성 #Cell에 다중범위 데이터 작성 ws.Range("A1:C1").Value = "t.. 2022. 10. 6.
DataFrame 생성할 때 If using all scalr values 오류 뜰 경우 [Pandas Library] import pandas as pd dict = { 'a' : 1, 'b' : 2 } df = pd.DataFrame(dict) 이런식으로 DataFrame을 만들경우 raise ValueError("If using all scalar values, you must pass an index" 라는 에러가 뜬다 해결 방법은 import pandas as pd dict = {'a' : 1, 'b' : 2 } df = pd.DataFrame.from_dict([dict]) df = pd.DataFrame.from_records([dict]) 방법으로 만들어주면 해결된다 2022. 10. 5.