笨办法学Python

打印:
%r
%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别。
%s => str(),比较智能
%r => repr(),处理较为简单和直接 

from datetime import datetime

s = "world"
print("hello %s (str)" %s)
print("hello %r (repr)" %s)

timeinfo = datetime.today()
print(str(timeinfo))
print("time is %s (str)" %timeinfo)
print(repr(timeinfo))
print("time is %r (repr)" %timeinfo)
结果:
hello world (str)
hello 'world' (repr)
2016-10-28 11:37:28.554000
time is 2016-10-28 11:37:28.554000 (str)
datetime.datetime(2016, 10, 28, 11, 37, 28, 554000)
time is datetime.datetime(2016, 10, 28, 11, 37, 28, 554000) (repr)

%r, repr就是对string的一个格式化处理,即,返回的是一个格式化处理过的string(新对象)

argv,可变参数:
#argv,可变参数(argument variable)
from sys import argv

#参数解包(unpack),将参数放进对应的变量中
script, first, secind, third = argv

print("The script is called:",script)
print("Your first variable is:",first)
print("Your second variable is:",secind)
print("Your third variable is:",third)
终端输入:python test1.py a b c
结果:
('The script is called:', 'test1.py')
('Your first variable is:', 'a')
('Your second variable is:', 'b')
('Your third variable is:', 'c')

argv的第一个参数是文件名,所以接收一个参数都是argv[1]

读写文件
open( )打开文件
close( )关闭兼保存文件
read( )读取文件内容
readline( )读取一行内容
write( )写入内容 
#with,简化了try…expect…finally,并且可以自动关闭文件
with open("xx.txt", "r") as f:
#read(),一次性将文件独进一个string中
print(f.read())
print(type(f.read()))

with open("xx.txt", "r") as f:
for line in f:
print(line)
#一行一行读取问价内容


列表
list_1 = ['a','b','b','c','d','e']
#pop(),删除最后一个元素
list_1.pop()
print(list_1)
#count(),统计这个元素
print(list_1.count("b"))
#remove(),删除指定元素
list_1.remove('b')
print(list_1)
#extend(),添加元素组或者当作两个列表相加,添加在原列表最后
list_1.extend(['q','t'])
print(list_1)
#append(),在列表最后添加元素
list_1.append('w')
print(list_1)
</span><span style="font-size:24px;">#insert(),元素插入指定位置
list_1.insert(1,'s')
print(list_1)
#reverse(),反向列表元素
list_1.reverse()
print(list_1)
#sort(),排序列表元素,可以有三个参数sorted(data, cmp=None, key=None, reverse=False),reverse参数是升序降序
list_1.sort()
print(list_1)
结果:
['a', 'b', 'b', 'c', 'd']
2
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'q', 't']
['a', 'b', 'c', 'd', 'q', 't', 'w']
['a', 's', 'b', 'c', 'd', 'q', 't', 'w']
['w', 't', 'q', 'd', 'c', 'b', 's', 'a']
['a', 'b', 'c', 'd', 'q', 's', 't', 'w'] 


web.py
目录结构:
gothonweb
|----bin
       |----app.py


第一个程序
编写app.py: 

import web

urls = (
'/','index',
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting

if __name__=="__main__":
app.run()
运行app.py

打开浏览器,http://127.0.0.1:8080/ 

控制台显示:

浏览器通过网络连接到127.0.0.1,连接成功后发起一个GET请求,访问URL / ,app.py中有URL与类的匹配关系,urls = ('/','index',) 中将'/'与index类联系在一起,web.py接收到访问/的请求会通过匹配关系找到index类,因为是一个GET请求所以会找到index.GET函数,程序执行这个函数返回响应给浏览器!

创建基本的模板文件
目录结构:
gothonweb
|----bin
       |----app.py
       |----templates
              |----index.html
编写index.html 
$def with(greeting)
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
$if greeting:
I just wanted to say<em style="color:green;font-size: 2em;">$greeting</em>.
$else:
<em>Hello</em>,World!
</body>
</html>


修改app.py

import web

urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')

class index(object):
def GET(self):
greeting = "Hello World"
return render.index(greeting=greeting)

if __name__=="__main__":
app.run()
打开浏览器,http://127.0.0.1:8080/


在app.py中添加一个web.template.render对象,用来指定模板目录位置,当浏览器出发index.GET后,返回一个模板,render.index(greeting=greeting)是render.index是找模板目录下的index.html文件,将参数传入模板,模板中有一个对参数的判断,又来决定显示什么内容,再最终内容返回响应,显示到浏览器

表单(form)
单个参数:
修改app.py 
import web

urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')

class index(object):
def GET(self):
form = web.input(name="Nobody")
greeting = "Hello %s"%form.name
return render.index(greeting=greeting)

if __name__=="__main__":
app.run()
打开浏览器,http://127.0.0.1:8080/

打开浏览器,http://127.0.0.1:8080?name=web


多个参数;
修改app.py 
import web

urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')

class index(object):
def GET(self):
form = web.input(name="Nobody", greet=None)
if form.greet:
greeting = "Hello %s, %s"%(form.name, form.greet)
return render.index(greeting=greeting)
else:
return "ERROR:GREET IS REQUIRED!"

if __name__=="__main__":
app.run()
浏览器访问:http://127.0.0.1:8080/?name=web

浏览器访问:http://127.0.0.1:8080/?name=web&greet=python

创建HTML表单
目录结构:
gothonweb
|----bin
       |----app.py
       |----templates
              |----index.html
              |----hello_form.html
新建一个hello_form.html 
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<form action="/" method="POST">
A Greeting:<input type="text" name="greet">
<br/>
Your Name:<input type="text" name="name">
<br/>
<input type="submit">
</form>

</body>
</html>

修改app.py
import web

urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')

class index(object):
def GET(self):
return render.hello_form()

def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s, %s"%(form.name, form.greet)
return render.index(greeting=greeting)

if __name__=="__main__":
app.run()
浏览器访问http://127.0.0.1:8080/


控制台:


其实这里还能看出HTTP的连接是短连接,每次都是客户端的随机端口重新发起一次连接

浏览器访问/,发起一次GET请求,app.py中index.GET返回了hello_form.html,当填好表单以后,通过form,将POST请求发给了index.POST做处理,再返回响应给浏览器

创建布局模板(类似其它的模板继承)
目录结构:
gothonweb
|----bin
       |----app.py
       |----templates
              |----index.html
              |----hello_form.html
              |----layout.html

修改index.html 
def with(greeting)

$if greeting:
I just wanted to say<em style="color:green;font-size: 2em;">$greeting</em>.
$else:
<em>Hello</em>,World!


修改hello_form.html

<form action="/" method="POST">
A Greeting:<input type="text" name="greet">
<br/>
Your Name:<input type="text" name="name">
<br/>
<input type="submit">
</form>


新建layout.html

$def with(content)

<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
$:content
</body>
</html>

修改app.py

import web

urls = (
'/','index',
)
app = web.application(urls, globals())
render = web.template.render('templates/', base="layout")

class index(object):
def GET(self):
return render.hello_form()

def POST(self):
form = web.input(name="Nobody", greet="Hello")
greeting = "%s, %s"%(form.name, form.greet)
return render.index(greeting=greeting)

if __name__=="__main__":
app.run()

原文地址:https://www.cnblogs.com/pscc/p/10014150.html