Python基础笔记
1.使用pydoc的方法,在Linux下 pydoc file/os/sys...;在windows下 python -m pydoc file/os/sys...
2.使用脚本参数, from sys import argv,
scriptname, 1st_arg, 2nd_arg, 3rd_arg = argv #参数被按序赋给左边的变量
3.格式化的写三行到文本中
file = open(filename, 'a')
line1 = raw_input("line1:")
line2 = raw_input("line2:")
line3 = raw_input("line3:")
file.write(("%s\n%s\n%s\n") % (line1,line2,line3))
4.file.flush() 是将缓存中的内容写到磁盘中,
一般调用write()函数不会立即写, 是file.close()的时候才写到磁盘中,
但是如果调用write之后立即调用flush会马上写进磁盘.
5.一句话拷贝文件
open(to_file, 'w').write(open(from_file).read())
不过这样似乎没办法关闭文件
6. file.seek()函数帮助重定位文件中的读取位置.
比方seek(0)又到文件起始处, 这时调用readline()读的就是第一行
7.将list转化成字符串, 用join
list = [ ]
list.append("hello")
list.append("world")
str =' '.join(list)
将字符串转化成list, 用split
list2 = str.split(' ')
8. 一个在字典中存放函数名的例子
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
# ok pay attention!
cities['_find'] = find_city
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
# this line is the most important ever! study!
city_found = cities['_find'](cities, state)
print city_found
启发就是:可以将一组起着不同作用的函数以字典的方式存储起来, 再按照需要适时调用.
9. 将百分数从字符形式转变成数字
比如str = '40%'
首先str = str[:-1]
这样 str = '40', 然后 num = int(str), 这样 num = 40
或者用string模块里面的string.atoi(str)也可以
10. list.append() 是将字符放进List中, append对象是字符
list.extend() 是将两个list拼接起来, extend对象是list
11. 创建一键多值的字典, 其实就是一个键配一个列表的值
比如{IP1:[usr1, pwd1],IP2:[usr2,pwd2]}
dict.setdefault(key,[ ]).append(value)
12. 将两个列表的值按序对应成一个字典
比如list1 = [1,2,3,4,5,6] list[2]=[a,b,c,d,e,f]
使用zip操作 dic1 = dict(zip(list1,list2))
dic1 = {1:a, 2:b, 3:c, 4:d, 5:e, 6:f}
如果不按序的对应创建, 可以:
dic2 = dict(zip(list1[::2], list2[1::2]))
dic2 = {1:b,3:d,5:f}
13.字典长度, 也可以用len()
在字典中iterate, 比如
it1 = iter(dic1)
it1.next()
14. 给函数写help的时候记得与正文对齐!
def func1 ( ):
'''this function is used for test'''
print "test!!"
2.使用脚本参数, from sys import argv,
scriptname, 1st_arg, 2nd_arg, 3rd_arg = argv #参数被按序赋给左边的变量
3.格式化的写三行到文本中
file = open(filename, 'a')
line1 = raw_input("line1:")
line2 = raw_input("line2:")
line3 = raw_input("line3:")
file.write(("%s\n%s\n%s\n") % (line1,line2,line3))
4.file.flush() 是将缓存中的内容写到磁盘中,
一般调用write()函数不会立即写, 是file.close()的时候才写到磁盘中,
但是如果调用write之后立即调用flush会马上写进磁盘.
5.一句话拷贝文件
open(to_file, 'w').write(open(from_file).read())
不过这样似乎没办法关闭文件
6. file.seek()函数帮助重定位文件中的读取位置.
比方seek(0)又到文件起始处, 这时调用readline()读的就是第一行
7.将list转化成字符串, 用join
list = [ ]
list.append("hello")
list.append("world")
str =' '.join(list)
将字符串转化成list, 用split
list2 = str.split(' ')
8. 一个在字典中存放函数名的例子
cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
def find_city(themap, state):
if state in themap:
return themap[state]
else:
return "Not found."
# ok pay attention!
cities['_find'] = find_city
while True:
print "State? (ENTER to quit)",
state = raw_input("> ")
if not state: break
# this line is the most important ever! study!
city_found = cities['_find'](cities, state)
print city_found
启发就是:可以将一组起着不同作用的函数以字典的方式存储起来, 再按照需要适时调用.
9. 将百分数从字符形式转变成数字
比如str = '40%'
首先str = str[:-1]
这样 str = '40', 然后 num = int(str), 这样 num = 40
或者用string模块里面的string.atoi(str)也可以
10. list.append() 是将字符放进List中, append对象是字符
list.extend() 是将两个list拼接起来, extend对象是list
11. 创建一键多值的字典, 其实就是一个键配一个列表的值
比如{IP1:[usr1, pwd1],IP2:[usr2,pwd2]}
dict.setdefault(key,[ ]).append(value)
12. 将两个列表的值按序对应成一个字典
比如list1 = [1,2,3,4,5,6] list[2]=[a,b,c,d,e,f]
使用zip操作 dic1 = dict(zip(list1,list2))
dic1 = {1:a, 2:b, 3:c, 4:d, 5:e, 6:f}
如果不按序的对应创建, 可以:
dic2 = dict(zip(list1[::2], list2[1::2]))
dic2 = {1:b,3:d,5:f}
13.字典长度, 也可以用len()
在字典中iterate, 比如
it1 = iter(dic1)
it1.next()
14. 给函数写help的时候记得与正文对齐!
def func1 ( ):
'''this function is used for test'''
print "test!!"