python杂乱笔记【命令行帮助&格式化字符串&seek函数】

0.首先是命令行的帮助

在命令行输入 pydoc raw_input

可以获取该方法的说明文档

按q退出。

另外file,open,os,sys都可以。

目测是获取帮助。


1格式化字符串

%d整数

%s字符串

%r不管什么都转换

一个例子

usingr = "\tusingr"
usings = usingr
print "using r %r" % usingr
print "using s %s" % usings

结果

python ex7plus.py
using r '\tusingr'
using s     usingr

-------------ending----------------

2.python中file的seek方法

代码:

from sys import argv
def print_all(f):
 	print f.read()
def rewind(f):
 	f.seek(int(argv[2]), int(argv[3]))
 	#argv[2] and argv[3] means arg inputted
 	#thirdly and secondly from terminal
def print_a_line(line_count, f):
	print line_count, f.readline()
current_file = open(argv[1])
print "first let's print the whole file:"
print_all(current_file)
print "now let's rewind, kind of like a tape"
rewind(current_file)
print "let's print three lines"
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_file.close()

在terminal中输入 python ex17.py 5 1

得到的运行结果是

first let's print the whole file:
123456789
granton
zhuang

now let's rewind, kind of like a tape
let's print three lines
1 6789                                          //从开头位置移动五个字符(所以到了6的位置)

2 granton

3 zhuang

seek中的两个参数,第一个表示移动的字符个数(可以为负数),

第二个参数:

取值为0的时候,表示从文件开头位置移动,

取值为1的时候表示从当前位置移动,

取值为2的时候表示从文件结尾位置移动

在terminal中输入 python ex17.py -5 2           //从文件末尾开始读

结果是:

first let's print the whole file:
123456789
granton
zhuang

now let's rewind, kind of like a tape
let's print three lines
1 uang                          //从文件的末尾倒着算5个,以此为起点

2

在terminal中输入 python ex17.py -5 1           //1表示从当前位置移动,

                                                                                  //由于在调用seek方法之前调用了read方法,

                                                                                  //读到了文件的末尾,所以效果与2的时候一样

first let's print the whole file:
123456789
granton
zhuang

now let's rewind, kind of like a tape
let's print three lines
1 uang

2
3
---------------分割线-----------------


3.打印10个星号

print "." * 10 #print ten '*'

4.将一个文件写进另一个文件中

#将一个文件写进另一个文件中
from sys import argv           
from os.path import exists
output = open(argv[2], 'w')           #参数二是目标文件
output.write(open(argv[1]).read())              #参数一是源文件
print "Alright, all done."
open(argv[2], 'w').close()
open(argv[1]).close()


5.清空一个文件

target = open(filename,"w")
target.truncate()

6.关于函数

def break_words(stuff):
       """thisfunction will break up words for us."""
       words= stuff.split(' ')

在定义方法的第一行中使用左右个三个引号,作为该方法的帮助文档。

7.关于for

elements = []         #定义一个空序列
for i in range(0,6):        #range生成一个0到5的序列,for执行循环
       print"adding %d to the list." % i
       elements.append(i)
for i in elements:           #elements序列等同于range(0,6)的返回值
       print"elements was: %d" % i
#所以以上写法等同于以下写法:
elements = range(0,6)
print elements


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值