题目要求如下:写一个程序,比较2个文本的内容,如果不同,显示不同的行数
def comp():
file1=input(‘please enter first name: ‘)
file2=input(‘please enter first name: ‘)
f1=open(file1)
f2=open(file2)
count=0
differ=[]
for line1 in f1: #这2行代码很关键,迭代f1的行数,以f1的行数数量为基准,读取f2的行数
line2=f2.readline() #这个方法很好
if line1 != line2: #如果内容不一样,则计数器加1
count+=1
differ.append(count) #把当前的计数数字迭代进differ的列表,其实他组后的形式是[1,2,3],内容无关紧要,因为最后我们是用len(differ)来看触发了几次计数
if len(differ)==0:
print (‘two files are same!’)
else:
print (‘There are %d: different places’% len(differ))
for each in differ:
print (‘At %d row is different ‘% each)
comp()
——————————————————————————————————————————————————————————————————————————
下一题
我自己设置的文本如下:
自己的代码如下:
def readfile():
filename=input(‘please enter the filename (E:\\mystuff\\test.txt) :’)
fo=open(filename)
number=int(input(‘please enter before which lines you want to read : ‘))
p=fo.readlines() #这里是用p指代所有读出来的行数,readlines返回的是一个list对象!!!
print ((‘%s first %d content is : ‘)%(filename,number)) #这里千万不要忘记括号啊,为了这括号,头疼了几十分钟
for each_line in p:
print (each_line,end=”)
fo.close()
readfile()
小甲鱼的代码如下:
def file_view(file_name, line_num):
print(‘\n文件%s的前%s的内容如下:\n’ % (file_name, line_num))
f = open(file_name)
for i in range(int(line_num)):
print(f.readline(), end= ”)
f.close()
file_name = input(r’请输入要打开的文件(C:\\test.txt):’)
line_num = input(‘请输入需要显示该文件前几行:’)
file_view(file_name, line_num)
看了几个小甲鱼的代码,小甲鱼一般喜欢把用于需要输入的命令代码,放在函数外边,而我自己比较喜欢一股脑全部放在里面,不知道哪个比较好,还是本就没有好坏之分。
————————————————————————————————————————————————————————————————————————————-
这道题目是建立在上一题的基础上的扩展
def readfile():
filename=input(‘please enter the filename (E:\\mystuff\\test.txt) :’)
fo=open(filename)
number=input(‘please enter before which lines you want to read :’)
if number.strip()==’:’: #
这3行我觉得根本就没必要……
begin=’1′ #
下面split函数已经将begin和end分开来了……
end=’-1′ #
(begin,end)=number.split(‘:’)
if begin==”:
begin=’1′ #
本来我这里填的是0,下面end也是填的是0,我的想法是0:0正好可以取全集,但是我忽略了一个问题…就是后面要算行数差的,如果我2个
if end==”: #
都填0的话,end减去begin后,等于还是0,就等于只读取了一行
end=’-1′
if begin==’1′ and end==’-1′: #
后来我想,如果用空字符串”来替代0行么?结论还是不行,因为后面要用int函数转换begin和end,然而,空字符串是不能转int的
prompt=’all’
elif begin==’1′:
prompt=’from beginning to %s’%end
elif end==’-1′:
prompt=’from %s to the end’%begin
else:
prompt=’from %s to %s’%(begin,end)
print ((‘\n the file %s %s as below: ‘)%(filename,prompt))
begin=int(begin)-1
end=int(end)
lines=end-begin
for i in range(begin):
fo.readline()
if lines<0:
print (fo.read())
else:
for n in range(lines):
print (fo.readline(),end=”)
fo.close()
readfile()
———————————————————————————————————————————————————————————————————————————– 最后一题:要求是替换字符串内容
def replace(filename,original,new):
f=open(filename) #
这里要记住,open文件出来的内容,他的可迭代类型,不是字符串,而是按照每一行来分割开来的,也就是,分割出来的是str,但是本身不是str!!
count=0
content=[] #本身不理解为什么要用content来做缓存,后来知道了,你无法在文件中直接修改,因为
open状态下用w模式的话,会直接覆盖掉原来的内容
for i in f:
if original in i:
count+=i.count(original) #
这里本来我是写的count+=1,后来想想是错的,因为可能一行出现关键字几次,但是按照count+=1的话,每行加1了只能。
i=i.replace(original,new)
content.append(i)
print (‘file %s has %d %s’%(filename,count,original))
print (‘Are you sure to replace all %s to %s’%(original,new))
choice=input(‘Yes/No :’)
if choice in [‘yes’,’Yes’,’YES’]:
p=open(filename,’w’)
p.writelines(content)
p.close()
f.close()
print(content)
filename=input(‘please enter the filename: ‘)
original=input(‘please enter the target word: ‘)
new=input(‘please enter the replacement word: ‘)
replace(filename,original,new)