Programming/Python

Programming/Python

UTF8 with BOM 으로 파일 인코딩해서 저장하기

최근 만들고 있는 자막 변환기 파이썬 프로그램을 통해 .srt 파일을 만드는 도중, subtitle editor에서 저장된 .srt 파일이 UTF8 with BOM 형태로 인코딩 되어 저장되는 사실을 알았다. 어도비 프리미어 프로에서 내가만든 파이썬 프로그램의 .srt 파일 결과물이 인식이 안되어 혹시 해당 인코딩 문제인 줄 알고 인코딩을 바꿔본 경험을 적어본다 (프리미어 프로에 .srt 파일이 인식 안되던 건 사실 인코딩 문제가 아니었다). out_put1 = open(os.path.join(save_path_info, file_name + "_자막.srt"), "w", encoding='utf8') ''' 해당 특문을 파일 최상단에 먼저 적어줘야 utf8 with bom으로 인식한다. 실 파일에는 해..

Programming/Python

Dictionary 데이터에서 키 값만 추출하기

최근 지인 부탁으로 파이썬으로 자막을 변환하는 gui 프로그램을 만들고 있는데, 그러다가 알게 된 사실을 메모해본다. 파이썬을 제대로 깊게 공부해본적은 없고 목적에 따라 필요한 부분만 배워서 쓰고 있다. 참고로 파이썬3를 쓰고 있다. dictionary 객체에 keys() 함수를 호출한 것을 list()로 감싸주면 키 값만 담긴 리스트가 생긴다. 이후 적절한 인덱스 값을 통해 키값을 가져오면 된다.

Programming/Python

[Python-Crawling] 정부 혁신 제안 사이트 크롤링 - 1

패키지 임포트¶ In [26]: # 필요한 도구를 불러온다. # 파이썬에서 사용할 수 있는 엑셀과 유사한 데이터분석 도구 import pandas as pd # 매우 작은 브라우저로 웹사이트의 내용과 정보를 불러올 수 있습니다. import requests # request로 가져온 웹사이트의 html 태그를 파싱하는데 사용합니다. from bs4 import BeautifulSoup as bs # 랜덤숫자를 생성한다. import random import time # 대량 데이터 처리시 진행 상황을 표시합니다. from tqdm import tqdm, trange # 정규표현식 import re 제안 연월 설정¶ In [27]: year_month = 201910 페이지 번호대로 제안 목록을 가져오는 ..

Programming/Python

[Python-Basic] Class Data Type - 4(Self)

class Foo: def func1(): print("function 1 is called") def func2(self): print(id(self)) print("function 2 is called") def func3(self): print(self.__str__) """ self is instance of class """ foo = Foo() foo.func2() print(id(foo)) Foo.func1() f3 = Foo() print(f3) print(id(f3)) print(Foo.func3(f3)) 실행결과 $ py self.py 3318160 function 2 is called 3318160 function 1 is called 3318424 None

Programming/Python

[Python-Basic] Class Data Type - 4(Namespace)

class Stock: market = "kospi" if(__name__ == '__main__'): # When class is define namespace is generated as class name # All of method and variable info is save in namespace as dictionary print('='*30) print(dir()) print('='*30) print(Stock.__dict__) print('='*30) # Instance is have own namespace s1 = Stock() s2 = Stock() print(dir()) print('='*30) print(id(s1)) print('='*30) print(id(s2)) print(..

Programming/Python

[Python-Basic] Class Data Type - 3(Inheritance)

class Parent: def can_sing(self): print('Sing a song') class LuckyChild(Parent): pass class UnluckyChlid: pass class LuckyChild2(Parent): def can_dance(self): print('Shuffle Dance') father = Parent() father.can_sing() child = LuckyChild() child.can_sing() child2 = UnluckyChlid() print(child.__dir__()) print(child2.__dir__()) child3 = LuckyChild2() child3.can_dance() child3.can_sing() 실행결과 $ py i..

Programming/Python

[Python-Basic] Class Data Type - 3(Constructor)

class BusinessCard: def __init__(self): self.name = 'empty' self.email = 'empty' self.addr = 'empty' def set_info(self, name, email, addr): self.name = name self.email = email self.addr = addr def show_info(self): print('-'*30) print('Name: ', self.name) print('E-mail: ', self.email) print('Address: ', self.addr) print('-'*30) card = BusinessCard() card.show_info() card.set_info('JohnMark','prac..

Programming/Python

[Python-Basic] Class Data Type - 2(Instance)

class Account: num_accounts = 0 def __init__(self, name): self.name = name Account.num_accounts +=1 def __del__(self): Account.num_accounts -=1 kim = Account('Chang') lee = Account('Kim') print(kim.name) print(lee.name) print(kim.num_accounts) 실행결과 $ py class_instance.py Chang Kim 2

JohnMark
'Programming/Python' 카테고리의 글 목록