易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : Python

Python入门的36个例子 之 23

源代码下载:下载地址在这里
# 026
aList = ['1','2','3','4']
aListCopy = aList # 其实,这里仅仅复制了一个引用
del aList[0]
print aList
print aListCopy # 两个引用指向了了同一个对象,所以打印结果一样
aListCopy = aList[:] # 这是复制整个对象的有效方法
del aList[0]
print aList
print aListCopy

output:
['2', '3', '4']
['2', '3', '4']
['3', '4']
['2', '3', '4'] ......

Python入门的36个例子 之 24

# 027
toolName = 'Google'
if toolName.startswith('Go'):
print 'The tool\'s name starts with \"Go\".'
if 'oo' in toolName:
print 'The tool\'s name contains the stirng "oo".'
print toolName.find('gl') # 返回首次出现“gl”字符串的索引
if toolName.find('Baidu'):
print 'Can\'t find "Baidu" in tool\'s name.'
aList = ['a','b','c']
print '...'.join(aList)
output:
The tool's name starts with "Go".
The tool's name contains the stirng "oo".
3
Can't find "Baidu" in tool's name.
a...b...c ......

Python入门的36个例子 之 25

源代码下载:下载地址在这里
# 028
consoleInput = raw_input('请输入点什么吧:')
aFile = file(r'C:\out.txt', 'w')
aFile.write(consoleInput + '\n')
aFile.write('这里是第二行')
aFile.close()

output:
>>>
请输入点什么吧:haha
>>>
......

Python入门的36个例子 之 26

源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()

output:
>>>
This is the first line.
This is the second line.
This is the third line.
>>> ......

Python入门的36个例子 之 27

源代码下载:下载地址在这里
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()

output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()

output:
e.g.3
实现根据原始文件有没有最后一行空行的情况来进行“完美添加”。
# 031
aFile = file(r'C:\temp.txt', 'r')
lastLine = ''
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
lastLine = line
# end of while
aFile.close()
aFile = file(r'C:\temp.txt', 'a')
if not lastLine.endswith('\n'): # 说明源文件没有一个空行,需要重新另起一行
aFile.write('\n')
# end of if
aFile.write('这是新添加的一行!')
aFile.close()

此时无论原始文件是e.g.1的样子还是e.g.2的样子,结果都是:
......

Python入门的36个例子 之 28

源代码下载:下载地址在这里
# 032
# 其实cPickle这个模块起到的作用可以用“完美地协调了文件中的内容(对象)和代码中的引用”来形容
import cPickle as p # 这条语句给cPickle起了个小名p
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close()
del aList # 现在已经没有aList所指的对象了,甚至没有aList这个引用了
f = file(objectFileName, 'r')
storedObject = p.load(f)
print storedObject

output:
>>>
[1, 2, 3]
>>>
当然,在文件中,Python采用了一种便于自己分辨对象类型的格式来存储:
......
总记录数:695; 总页数:116; 每页6 条; 首页 上一页 [102] [103] [104] [105] 106 [107] [108] [109] [110] [111]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号