日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python文件中单词的删除_使用python删除文件中的多余单词

發布時間:2023/12/20 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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删除文件中的多余单词的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。