IT/Python30 파일을 만들기 전에 파일 존재 확인하는 코드 파일을 쓰기 전에 파일이 있는지 확인하는 방법 import osif not os.path.exists('filename'): with open('filename', 'wt') as f: f.write('This is a test')else: print('File exists') 실수로 파일을 덮어쓰는 경우를 방지하기 위해서 한다. 2014. 12. 19. python beautifulsoap - table parsing 친구의 노가다를 조금 덜어주기 위해 파이썬으로 웹에 있는 테이블을 파싱해서 엑셀로 만들어주는 소스를 짜봤다. 아래의 사이트에서 테이블에 있는 데이터를 엑셀로 저장하는 것이다. 노가다로 하나씩 복사 붙여넣기 할 수 없으니까... python으로 웹파싱이 쉬운 것을 생각했다. Beautiful Soup 를 사용했다. beautifulsoup는 웹파싱하기에 쉽고 간단하다. 아래 사이트에서 다운 받는다. http://www.crummy.com/software/BeautifulSoup/ 이용할 라이브러리 - BeautifulSoup, csv, urllib2 # -*- coding: utf-8 -*-import csvimport urllib2import BeautifulSoup url = 'http://해당 웹사이.. 2014. 12. 19. python txt파일 읽기 에러 'cp949' UnicodeDecodeError: 'cp949' codec can't decode bytes in position : illegal multibyte sequence python3 부터는 ANSI 기준으로 작성된 파일만 읽을 수 있다. UTF-8로 작성된 파일은 보통 방법으로 읽을 때 에러가 난다. 2가지 방법이 있다. 1. utf-8을 붙여준다. f = open( "text.txt", "r", "utf-8" ) 2. 파일의 인코딩을 ANSI로 바꾸면 된다.해당 파일을 메모장으로 열었을 경우 [다른 이름으로 저장]에서 가능하다.밑에 보면 나와있다. 인코딩: ~~ 2014. 12. 19. sklearn.cross_validation.train_test_split(*arrays, **options) sklearn.cross_validation.train_test_split(*arrays, **options) 함수는배열 또는 매트릭스를 랜덤하게 트레인과 테스트 셋으로 나눈다. 사용법: Split into training and test set (e.g., 80/20) 80대 20으로 나눈다. from sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) Examples>>>>>> import numpy as np >>> from sklearn.cross_validation import train_test_spl.. 2014. 12. 19. 이전 1 2 3 4 5 6 7 8 다음 반응형