목록코딩 (418)
Creative Code
# urllib 모듈에서 request 함수를 가져옵니다. from urllib import request # os 모듈을 가져옵니다. import os # 다운로드할 파일의 URL을 변수에 할당합니다. url = '여기에_다운로드할_파일의_URL을_입력하세요' # 파일을 다운로드할 디렉토리 경로를 설정합니다. path = os.path.dirname(__file__) + '/down/' # 만약 디렉토리가 존재하지 않으면, 디렉토리를 생성합니다. if not os.path.exists(path): os.makedirs(path) # 다운로드한 파일의 로컬 파일명을 생성합니다. filename = path + url.split('/')[-1] # URL로부터 파일을 다운로드하여 로컬에 저장합니다. req..
import json with open('study/json_example.json','r',encoding = 'utf8')as f: contents = f.read() json_data= json.loads(contents) print(json_data['employees']) dict_data = {'Name':'Zara','Age':7,'Class':'First'} with open('study/data.json','w') as f: json.dump(dict_data,f)
import re # 정규 표현식을 사용하기 위한 모듈 임포트 import urllib.request # URL에서 데이터를 가져오기 위한 모듈 임포트 # 이미지 다운로드를 위한 URL url = 'https://images.unsplash.com/photo-1531306728370-e2ebd9d7bb99?auto=format&fit=crop&q=80&w=1000&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxleHBsb3JlLWZlZWR8Mnx8fGVufDB8fHx8fA%3D%3D' # 이미지 다운로드 시작 메시지 출력 print('이미지 다운로드 시작') # URL로부터 이미지 다운로드 및 'space.jpg' 파일로 저장 fname, header = urllib.request.url..
# 줄 카운터 및 데이터 저장을 위한 빈 리스트 초기화 line_counter = 0 data_header = [] customer_list = [] # "study/customers.csv" 파일을 열고 읽어들입니다 with open("study/customers.csv") as customer_data: while 1: data = customer_data.readline() if not data: break # 첫 번째 라인인 경우, 헤더로 분할하여 저장 if line_counter == 0: data_header = data.split(',') else: # 그렇지 않으면 데이터를 분할하고 customer_list에 추가 customer_list.append(data.split(',')) line..
# 0에서 19까지의 i 값으로 반복합니다. for i in range(20): try: # 20을 i로 나눈 값을 출력하려고 시도합니다. print(20/i) except ZeroDivisionError: # 만약 0으로 나누는 경우 ZeroDivisionError가 발생하면 예외 처리됩니다. print('division by zero error') #IndexError : 리스트의 인덱스 범위를 넘어갈 때 #NameError : 존재하지 않는 변수를 호출할 때 #ZeroDivisionError : 0으로 숫자를 나눌 때 #ValueError : 변환할 수 없는 문자나 숫자를 변환할 때 #FileNotFoundError : 존재하지 않는 파일을 호출할 때 # IndexError 예외 처리 예제 arr ..
# random 모듈을 import하여 난수 생성과 관련된 함수들을 사용합니다. import random # random.randint() 함수를 사용하여 1에서 1000 사이의 무작위 정수를 출력합니다. print(random.randint(1, 1000)) # random.randint() 함수를 사용하여 0에서 100 사이의 무작위 정수를 출력합니다. print(random.randint(0, 100)) # random.random() 함수를 사용하여 0과 1 사이의 무작위 부동 소수점 숫자를 출력합니다. print(random.random()) # time 모듈을 import하여 시간과 관련된 함수들을 사용합니다. import time # time.localtime() 함수를 사용하여 현재 시간을..