%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)
#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)
#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)
#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)
import web
urls = (
'/','index',
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__=="__main__":
app.run()
$def with(greeting)
Test
$if greeting:
I just wanted to say$greeting.
$else:
Hello,World!
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/
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/
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
Hello
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/
def with(greeting)
$if greeting:
I just wanted to say$greeting.
$else:
Hello,World!
$def with(content)
$:content
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()