易截截图软件、单文件、免安装、纯绿色、仅160KB

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 Re

1. Basic
参考《Python正则表达式操作指南》 
模块re,perl风格的正则表达式
regex并不能解决所有的问题,有时候还是需要代码
regex基于确定性和非确定性有限自动机
2. 字符匹配(循序渐进)
元字符
. ^ $ * + ? { [ ] \ | ( )
1) "[" 和 "]"常用来指定一个字符类别,所谓字符类别就是你想匹配的一个字符集。如[ ......

python运算符 供重载参考

二元运算符及其对应的特殊方法
二元运算符
特殊方法
+
__add__,__radd__
-
__sub__,__rsub__
*
__mul__,__rmul__
/
__div__,__rdiv__,__truediv__,__rtruediv__
//
__floordiv__,__rfloordiv__
%
__mod__,__rmod__
**
__pow__,__rpow__
<<
__lshift__,__rlshift__
>>
_ ......

Python模块学习

  StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO。一个简单的例子,让你对StringIO有一个感性的认识: 1  #coding=gbk 2    3  import StringIO, cStringIO, sys 4    5  s ......

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 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号