python

Development

[Python] 디렉토리 파일 목록 받아오기

Python으로 디렉토리 내부에 있는 파일 목록을 받아오는 기능을 개발해 보았다. import os def list_files(directory): file_list = [] for root, dirs, files in os.walk(directory): for file in files: file_path = os.path.join(root, file) file_list.append(file_path) return file_list # Example usage # directory_path = "/path/to/directory" directory_path = "C:\Python34" # windows files = list_files(directory_path) for file in files: pri..

Development

[Python] 피보나치 수열

이번에는 파이썬으로 피보나치 수열을 개발해 보았다. 피보나치 수열에 대한 설명이 필요하다면 링크 참고. def fibonacci_numbers(n): a, b = 0, 1 for _ in range(n): print(a, end=' ') a, b = b, a + b print("Fibonacci Numbers:") n = int(input("Enter the number of terms: ")) fibonacci_numbers(n)

Development

[Python] 간단한 계산기

파이썬 기본 문법에 익숙해지기 위해 이것 저것 개발해보고 있다. 이번에는 간단한 계산기를 개발해보았다. 너무 간단하지만 기본 문법이 익숙해져야 하니.. def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(a, b): return a / b print("Simple Calculator") while True: print("0. Quit") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") choice = input("Enter your choice (0-4)..

Development

[Python] 텍스트 파일에서 단어 수 세기

파이썬을 공부하며 이것저것 개발해보고 있다. 다른 언어로 같은 기능을 개발했을 때는 훨씬 복잡했던 것 같은데.. 파이썬으로 작성하니 코드가 매우 간단하다. filename = "data/sample.txt" wordcount = {} with open(filename, 'r') as f: for line in f: words = line.split() for word in words: word = word.lower() # 소문자로 단어를 세고 싶은 경우 if word not in wordcount: wordcount[word] = 1 else: wordcount[word] += 1 for key, value in wordcount.items(): print(key, value) 결과는 아래와 같다. i..

Development

[Python] Oracle DB 쿼리 시 Dictionary 형태로 조회하기

Python에서 oracledb 통해 쿼리 시 응답의 기본적인 형태는 Tuple (1, 'Setting up a home network', 'Technology') 이런 형태를 편하게 다루기 위해서는 index를 사용해야 하므로 관리가 어렵다. id = row[0] title = row[1] category = row[2] 찾아보니 cursor의 rowFactory라는 메소드를 오버라이딩 하면 리턴받는 데이터의 형태를 바꿀 수 있다. def make_dic_factory(cursor): column_names = [d[0] for d in cursor.description] def create_row(*args): return dict(zip(column_names, args)) return create..

Development

OpenAI(ChatGPT) API로 블로그 발행하기 - #5. Python으로 Blogger 발행하기 (Google OAuth API)

0. Todo Google Cloud 프로젝트 생성 Blogger API 사용 설정 OAuth 클라이언트 ID 생성 Python으로 Blogger 서비스 연동하기 Python으로 Blog 발행하기 1. Google Cloud 프로젝트 생성 ① Google Cloud 계정 생성 (https://cloud.google.com/) ② Google Cloud Console로 이동 (https://console.cloud.google.com/) ③ 프로젝트 생성 2. Blogger API 사용 설정 ① API 라이브러리 페이지로 이동 (API 및 서비스 → 라이브러리) ② Blogger 검색 ③ Blogger API 사용 설정 이미 사용중이라 아래와 같이 '관리'라 나오지만 원래는 '사용'이라는 버튼이 있다. 3..

삿뿐삿뿐
'python' 태그의 글 목록