Python “如何跳过”;名称“;从文件中读取文本时?
我正在写一个程序,在这个程序中,我应该从一个文件中读取,跳过所有人的名字,并处理其他信息 我应该使用什么逻辑来跳过读取名称 我从一个文件中读取单词,然后用它们出现的频率制作单词云。 对于一些琐碎的事情,比如文章,我列了一个列表,并确保若读到的单词在这个文章列表中,它们不会被计算在内 但是我不明白我怎么能不读名字Python “如何跳过”;名称“;从文件中读取文本时?,python,file,python-2.7,input,Python,File,Python 2.7,Input,我正在写一个程序,在这个程序中,我应该从一个文件中读取,跳过所有人的名字,并处理其他信息 我应该使用什么逻辑来跳过读取名称 我从一个文件中读取单词,然后用它们出现的频率制作单词云。 对于一些琐碎的事情,比如文章,我列了一个列表,并确保若读到的单词在这个文章列表中,它们不会被计算在内 但是我不明白我怎么能不读名字 WordList=[] with open('file.txt','r') as f: for line in f: for word in line.spli
WordList=[]
with open('file.txt','r') as f:
for line in f:
for word in line.split():
if len(word)>3:
if word not in IgList:
WordList.append(word.lower())
# Get a set of unique words from the list
word_set =[]
for word in WordList[::-1]:
if word not in word_set:
word_set.append(word)
# create your frequency dictionary
freq = {}
# iterate through them, once per unique word.
for word in word_set:
freq[word] = WordList.count(word) / float(len(WordList))
size=[]##Size of each word is stored here
for i in word_set:
size.append(100*freq[i])
for i in range(0,len(word_set)):
print size[i],word_set[i]
x
是名称
之后的索引号,假设您知道文件中的名称在哪里。基本上它会跳过名字。例如,如果您的文件是这样的
John 25 USA
Mary 26 Bangladesh
Usain 63 Republic of the Congo
63 Republic of the Congo Usain
26 Bangladesh Mary
25 USA John
你必须写作
print (rd[1:])
或者如果是这样的话
John 25 USA
Mary 26 Bangladesh
Usain 63 Republic of the Congo
63 Republic of the Congo Usain
26 Bangladesh Mary
25 USA John
你必须打字
print (rd[:1])
假设句子通常以冠词开头,“姓名”以大写字母开头
IgList=list of articles
with open('file.txt','r') as f:
for line in f:
for word in line.split():
if word not in IgList:
if word[0] not in word.upper():##Cheking if first letter is Capital
WordList.append(word.lower())
如果一个单词以大写字母开头,它将被跳过。
可以编写额外的代码来跳过第一个阅读单词。您介意分享您的工作吗?您正在处理什么样的信息?我正在使用pygame库使用python创建一个单词云。我想您的意思是您正在计算文本中单词的频率,但忽略了名称-如果是这样,我建议检查每个单词是否正确在字典中忽略那些不是的。是的,我已经做了。检查上面,但是如何跳过“名称”,专有名词。我不知道名词的位置,文件是随机的。你说的“随机”是什么意思?任何文本文件都可以输入,名称可以在这些文件中的任何地方。你应该做一个人工智能