python文件中单词的删除_使用python删除文件中的多余单词
嗨,我正在學習Python,出于好奇,我編寫了一個程序來刪除文件中多余的單詞。
我正在比較文件text1.txt中的測試。和‘text2.txt’,基于text1中的測試,我刪除了test2中多余的單詞。在# Bin/ Python
text1 = open('text1.txt','r')
text2 = open('text2.txt','r')
t_l1 = text1.readlines()
t_l2 = text2.readlines()
# printing to check if the file contents were read properly.
print ' Printing the file 1 contents:'
w_t1 = []
for i in range(len(t_l1)):
w_t1 = t_l1[i].split(' ')
for j in range(len(w_t1)):
print w_t1[j]
#printing to see if the contents were read properly.
print'File 2 contents:'
w_t2 = []
for i in range(len(t_l2)):
w_t2.extend(t_l2[i].split(' '))
for j in range(len(w_t2)):
print w_t2[j]
print 'comparing and deleting the excess variables.'
i = 1
while (i<=len(w_t1)):
if(w_t1[i-1] == w_t2[i-1]):
print w_t1[i-1]
i += 1
# I put all words of file1 in list w_t1 and file2 in list w_t2. Now I am checking if
# each word in w_t1 is same as word in same place of w_t2 if not, i am deleting the
# that word in w_t2 and continuing the while loop.
else:
w.append(str(w_t2[i-1]))
w_t2.remove(w_t2[i-1])
i = i
print 'The extra words are: '+str(w) +'\n'
print w
print 'The original words are: '+ str(w_t2) +'\n'
print 'The extra values are: '
for item in w:
print item
# opening the file out.txt to write the output.
out = open('out.txt', 'w')
out.write(str(w))
# I am closing the files
text1.close()
text2.close()
out.close()
說text1.txt文件有“生日快樂親愛的朋友”的字樣
text2.txt上寫著“祝你生日快樂,我親愛的朋友”
程序應該在text2.txt中給出額外的單詞,即“鼓掌,給,你,我的,最好”
上面的程序運行得很好,但是如果我必須對一個包含數百萬字或百萬行的文件執行此操作呢??檢查每個單詞似乎不是一個好主意。我們有沒有Python預定義的函數呢??在
注:如果這是一個錯誤的問題,請原諒我,我正在學習python。很快我就不再問這些了。在
總結
以上是生活随笔為你收集整理的python文件中单词的删除_使用python删除文件中的多余单词的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑常用快捷键与命令
- 下一篇: python代码实现时间从12小时制到2