读书《Python基础教程》

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1 基础知识

2 列表和元组

3 使用字符串

4 字典

5 条件循环和其他语句

6 抽象

7 更加抽象

8 异常

9 方法属性迭代器

10 充电时刻

11 文件和素材

import re


f1 = open('/prj/python/b02/infile.txt', 'a')

f1.write('test python file1\n')

f1.flush

f1.writelines('test lines')

f1.close()

print('write file is ok.')


f = open('/prj/python/b02/infile.txt')

src = f.read()

print(src)

print('read file is ok.')


pos = f.tell()

f.seek(1, 0 - pos)

print(f.readlines())

print('read lines is ok.')


pos = f.tell()

f.seek(1, 0 - pos)

count = 0

for s in f.readlines():

 li = re.findall("test", s)

 if len(li) > 0:

  count = count + len(li)


print "Search:", count, "test"

f.close()


r = r'test'

s = len(re.findall(r, src))

print(s)


fa = open('/prj/python/b02/infile.txt')

fb = open('/prj/python/b02/outfile.txt', 'a+')

for s in fa.readlines():

    fb.write(s.replace('test', 'tst'))

fa.close()

fb.close()


12 图形用户界面

http://wxpython.org/

import wx

app = wx.App()

win = wx.Frame(None, title="Simple Editor", size=(410, 335))

win.Show()


#btn = wx.Button(win)

loadbtn = wx.Button(win, label='Open', pos=(225, 5), size=(80,25))

savebtn = wx.Button(win, label='Save', pos=(315, 5), size=(80,25))


filename = wx.TextCtrl(win, pos=(5, 5), size=(210, 25))

contents = wx.TextCtrl(win, pos=(5, 35), size=(390, 260), style=wx.TE_MULTILINE | wx.HSCROLL)

app.MainLoop()


13 数据库支持

import sqlite3


def convert(value):

    if value.startswith('~'):

        return value.strip('~')

    if not value:

        value = '0'

    return float(value)


conn = sqlite3.connect('food.db')

curs = conn.cursor()



curs.execute('''

CREATE TABLE food (

id    TEXT PRIMARY KEY,

desc  TEXT,

water FLOAT,

kcal  FLOAT,

protein FLOAT,

fat   FLOAT,

ash   FLOAT,

carbs FLOAT,

fiber FLOAT,

sugar FLOAT

)

''')


field_count = 10

query = 'INSERT INTO food VALUES(?,?,?,?,?,?,?,?,?,?)'


for line in open('/prj/python/b02/ABBERV.txt'):

    fields = line.split('^')

    vals = [convert(f) for f in fields[:field_count]]

    curs.execute(query, vals)


conn.commit()


query ='SELECT * FROM food'

print query

curs.execute(query)

names = [f[0] for f in curs.description]

for row in curs.fetchall():

    for pair in zip(names, row):

        print '%s: %s' % pair

    print


conn.close()


14 网络编程

from urllib import urlopen

webpage = urlopen('http://www.oschina.net/')

text = webpage.read()

print text



15 python和互联网

16 测试

17 扩展

18 程序打包

setup.py


编程

即时标记

绘画

XML

新闻

聊天

CGI

电子公告

文件共享

文件共享(GUI)

街机游戏


转载于:https://my.oschina.net/dubenju/blog/402204

你可能感兴趣的:(读书《Python基础教程》)