상용 python 인 코딩
3115 단어 python
import random # ,
rnd = random.randint(1,500)# 1-500
2。파일 읽 기
f = open("c:\\1.txt","r")
lines = f.readlines()#
for line in lines
print line
3。 서 류 를 쓰다
f = open("c:\\1.txt","r+")#
f.write("123")#
4。정규 표현 식, tomcat 로 그 를 읽 고 날 짜 를 인쇄 합 니 다.
import re
regx = "\d\d\d\d-\d\d-\d+"
f = open("c:\stdout.log","r")
i = 0
for str in f.readlines():
if re.search(regx,str):
Response.write(str+"<br>")
if i>10:break# ,
i=i+1
f.close();
5。데이터베이스 연결
import pgdb
conn = pgdb.connect
(host='localhost',databse='qingfeng',user='qingfeng',password='123')
cur = conn.cursor()
cur.execute("select * from dream")
print cur.rowcount
6。SAX 처리 xml:
import string
from xml.sax import saxlib, saxexts
class QuotationHandler(saxlib.HandlerBase):
"""Crude sax extractor for quotations.dtd document"""
def __init__(self):
self.in_quote = 0
self.thisquote = ''
def startDocument(self):
print '--- Begin Document ---'
def startElement(self, name, attrs):
if name == 'quotation':
print 'QUOTATION:'
self.in_quote = 1
else:
self.thisquote = self.thisquote + '{'
def endElement(self, name):
if name == 'quotation':
print string.join(string.split(self.thisquote[:230]))+'...',
print '('+str(len(self.thisquote))+' bytes)
'
self.thisquote = ''
self.in_quote = 0
else:
self.thisquote = self.thisquote + '}'
def characters(self, ch, start, length):
if self.in_quote:
self.thisquote = self.thisquote + ch[start:start+length]
if __name__ == '__main__':
parser = saxexts.XMLParserFactory.make_parser()
handler = QuotationHandler()
parser.setDocumentHandler(handler)
parser.parseFile(open("sample.xml"))
parser.close()
7. python 의 GUI 모듈 표준 은 Tkinter 이 고 QT 와 MFC 모듈 도 있 습 니 다. 관심 있 는 분 들 은 직접 검색 해 보 세 요.
import Tkinter
root=Tkinter.Tk()
my=Label(root,"Welcome to python's world")
my.pack()
root.mainloop()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.