-2
可能重複:
How to search and replace text from one file to another using Python?如何使用Python關鍵字替換和附加文件中的URL?
我有file1.txt
:
<echo http://photobucket.com/98a267d32b056fb0a5c8c07dd4c35cc5.jpg ?>
http://lincoln.com/view/filename1.jpg
http://lincoln.com/banner1/filename2.jpg
http://lincoln.com/banner1/filename3.jpg
而且我有file2.txt
:
http://lincoln.com/banner2/filename1.jpg
http://lincoln.com/banner2/filename2.jpg
我想:
如果file1中存在,但不是在文件2名:
remove line have filename
否則,如果文件名的文件1和file2存在:
the version in file2 replaces the line in file1
否則,如果文件名中file2存在,但沒有文件1:
do nothing
每一個幫助我的代碼吧!謝謝!
我想這代碼: 你能不能幫我修改我的代碼!
def file_merge(file1,file2):
file1contents = list()
file2contents = list()
file1=open('file1.txt','r')
for line in file1:
line= line.replace('\n','')
line= line.split('/')
file1contents.append(line)
file1.close()
file2=open('file2.txt','r')
for line in file2:
line = line.replace('\n','')
line = line.split('/')
file2contents.append(line)
file2.close()
file3contents=file1contents
for x in file2contents:
for y in file1contents:
if x[-1] == y[-1] and x[2]==y[2]:
file3contents[file3contents.index(y)]=x
here I want code :if filename exists in file1 but not in file2:
remove line have filename in file 1
file3 = open('out.txt','w')
for line in file3contents:
file3.write(str('/'.join(line))+'\n')
file3.close()
file_merge('file1.txt','file2.txt')
'remove line have filename' from where?從這兩個文件?只從file1? – joaquin 2011-12-17 16:32:12
刪除行在file1中有文件名!謝謝 – j3oy9x 2011-12-17 17:03:41