-
<PYTHON>[BeautifulSoup]Flower in my dev/Python 2015. 4. 27. 18:32
설치 pip install BeautifulSoup4 파싱방법 soup.title # The Dormouse's story soup.title.name # u'title' soup.title.string # u'The Dormouse's story' soup.title.parent.name # u'head' soup.p # The Dormouse's story soup.p['class'] # u'title' soup.a # Elsie soup.find_all('a') # [Elsie, # Lacie, # Tillie] soup.find(id="link3") # Tillie URL 추출 for link in soup.find_all('a'): print(link.get('href')) 샘플 웹페이지 파싱 ..
-
<PYTHON>특정 날짜가 지나면 파일을 지우기Flower in my dev/Python 2015. 4. 27. 18:23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #-*- coding:utf-8 -*- import os from datetime import date, timedelta, time, datetime filePath = '경로' days = 60 def listUp(): fileList = os.listdir(filePath) return fileList def date_delta(base_date): yy = int(base_date[:4]) mm = int(base_date[5:7]) dd = int(base_date[8:10]) d = date(yy,mm,dd) return (date.t..
-
<PYTHON>이더레이터?? 제너레이터??Flower in my dev/Python 2015. 4. 23. 11:44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 >>> a = [0,1,2,3,4,5,6,7,8,9] >>> b = iter(a) >>> b.next() 0 >>> b.next() 1 >>> b.next() 2 >>> b.next() 3 >>> b.next() 4 >>> b.next() 5 >>> b.next() 6 >>> b.next() 7 >>> b.next() 8 >>> b.next() 9 >>> b.next() Traceback (most recent call last): File "", line 1, in StopIteration cs 이더레이터.. 리스트 == [iter()] ==> 이더레이터 이더레이터.next() :..
-
<PYTHON> TCP_Echo_ServerFlower in my dev/Python 2015. 4. 21. 15:10
1 2 3 4 5 6 7 8 9 10 11 12 13 from twisted.internet import protocol, reactor class EchoTCP(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return EchoTCP() print "Starting server." reactor.listenTCP(9001, EchoFactory()) reactor.run() Colored by Color Scripter cs
-
<PYTHON> UDP_Echo_ServerFlower in my dev/Python 2015. 4. 21. 15:04
1 2 3 4 5 6 7 8 9 10 11 12 from twisted.internet.protocol import DatagramProtocol from twisted.internet import reactor class EchoUDP(DatagramProtocol): def datagramReceived(self, datagram, address): print "Received from address: " + str(address) print str(datagram) self.transport.write(datagram, address) print "Starting server." reactor.listenUDP(9001, EchoUDP()) reactor.run() Colored by Color S..
-
<PYTHON>시스템 정보 가져오기(CPU, RAM, DISK)Flower in my dev/Python 2015. 4. 21. 14:03
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 import time, random import psutil import os # Return CPU temperature as a character string def getCPUTemperature(): res = os.popen('vcgencmd measure_temp').readline() return(res.replace("temp=","").replace("'C\n","")) # Return RAM information (unit=kb) in a list # Index 0: total R..
-
<PYTHON>[loop]Flower in my dev/Python 2015. 4. 14. 14:11
-사전 반복 iterkeys() itervalues() iteritems() -파일 반복 f = open('test.txt') for line in f: print line -숫자 반복 count() for n in count(10): print n 10부터 끝없이 출력된다. -객체 반복 cycle() for n in cycle(['a','b','c']): print n -발생자 generate_ints() for n in generate_ints(5): print n 0 1 2 3 4 5 -짝,홀 반복 for n in range(1, 10, 2): print n for n in range(2, 10, 2): print n 리스트로 묶기 list = [n for n in range(100) if n % ..
-
<PYTHON>[리스트, 튜플, 사전]Flower in my dev/Python 2015. 4. 13. 16:24
리스트 a = [1,2,3,'flower'] 리스트 중첩 a = [1,2,3,'flower', ['rose','daisy'] ] a.append() 자료를 리스트 끝에 추가 a.insert() 자료를 지정된 위치에 삽입 a.index() 요소 검색 인덱스 반환 a.count() 요소 개수 알아내기 a.sort() 리스트 정렬 a.reverse() 자료 순서 바꾸기 a.remove() 지정 자료 값 한 개 삭제 a.pop() 리스트의 지정된 값 하나를 읽어 내고 삭제 a.extend() 리스트 추가 스택 list.append() list.pop() 큐 list.append() list.pop(0) 튜플 a = (1,2,3,'flower') 사전 a = {1:'flower', 2:'sky', 3:'earth..