Python 不读取我的文件,我正在搜索文本文件中出现的单词



我正在搜索文本文件中不同单词的出现情况。

我不擅长python,但我在google colab上做过。

import os
from google.colab import drive
drive.mount('/content/drive/', force_remount=True)
if not os.path.exists('/content/drive/My Drive/Miserables'):
os.makedirs('/content/drive/My Drive/Miserables')
root_dir = '/content/drive/My Drive/Miserables/'
os.listdir('/content/drive/My Drive/Miserables')
with open("/content/drive/My Drive/Miserables/miserable.txt", 'r') as f:
myString = f.readlines()
print(len(myString))
searchWords = ["example"]
for word in searchWords:
print(f"Word '{word}' appeared {myString.count(word)} time/s.")

问题是python实际上并不计算单词的数量,当我知道这些单词实际上存在于文本中时,结果为0。

有人能帮我一下吗?谢谢你。

我猜问题是你使用f.readlines()来获取文件内容。

该函数返回文件中每行的列表。

["foo foo faa", "faa foo faa"]

这表示你正在搜索这个列表中的单词。

试试f.read()

f.readlines()给出了代表行的每个元素的列表。

例如,如果文本是:

I'm here
this is here too
Bye buddy

会给你列表:

[
"I'm here",
"this is here too",
"Bye buddy"
]

要解决这个问题,请使用f.read()而不是f.readlines()

import os
from google.colab import drive
drive.mount('/content/drive/', force_remount=True)
if not os.path.exists('/content/drive/My Drive/Miserables'):
os.makedirs('/content/drive/My Drive/Miserables')
root_dir = '/content/drive/My Drive/Miserables/'
os.listdir('/content/drive/My Drive/Miserables')
with open("/content/drive/My Drive/Miserables/miserable.txt", 'r') as f:
myString = f.read()
print(len(myString))
searchWords = ["example"]
for word in searchWords:
print(f"Word '{word}' appeared {myString.count(word)} time/s.")

循环:

import os
from google.colab import drive
drive.mount('/content/drive/', force_remount=True)
if not os.path.exists('/content/drive/My Drive/Miserables'):
os.makedirs('/content/drive/My Drive/Miserables')
root_dir = '/content/drive/My Drive/Miserables/'
os.listdir('/content/drive/My Drive/Miserables')
with open("/content/drive/My Drive/Miserables/miserable.txt", 'r') as f:
myString = f.readlines()
print(len(myString))
searchWords = "example"
count = 0
for i in myString:
count += i.count(searchWords)

print(f"Word '{word}' appeared {count} time/s.")

最新更新