Word frequency counter in file
I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16...
but it outputs this instead 'A':2...'a':14
. Here is my code. Any help would be much appreciated.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word not in wordcount:
wordcount[word]=1
else:
wordcount[word]+=1
print(wordcount)
python dictionary file-io
add a comment |
I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16...
but it outputs this instead 'A':2...'a':14
. Here is my code. Any help would be much appreciated.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word not in wordcount:
wordcount[word]=1
else:
wordcount[word]+=1
print(wordcount)
python dictionary file-io
add a comment |
I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16...
but it outputs this instead 'A':2...'a':14
. Here is my code. Any help would be much appreciated.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word not in wordcount:
wordcount[word]=1
else:
wordcount[word]+=1
print(wordcount)
python dictionary file-io
I am working on an assignment and I have hit a wall. The assignment requires me to count the frequency of words in a text file. I got my code to count the words and put them into a dictionary but cannot put words together if they have different cases. For example I need the output to show 'a':16...
but it outputs this instead 'A':2...'a':14
. Here is my code. Any help would be much appreciated.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word not in wordcount:
wordcount[word]=1
else:
wordcount[word]+=1
print(wordcount)
python dictionary file-io
python dictionary file-io
edited Nov 12 at 6:27
petezurich
3,49581733
3,49581733
asked Nov 12 at 6:05
gbenyu
224
224
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Seems like in the question your saying there is a uppercase and lowercase issue, so why not:
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word.lower() not in wordcount:
wordcount[word.lower()]=1
else:
wordcount[word.lower()]+=1
print(wordcount)
Or:
file=open("phrases.txt","r")
wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
for word in file.read().split():
wordcount[word.lower()]+=1
print(wordcount)
add a comment |
You can use an inbuilt function called Counter
for this as an alternative to looping through the list.
example :
from collections import Counter
file = open("phrases.txt","r")
data = file.read().lower().split() # added lower() will convert everything to lower case
wordcount = dict(Counter(data))
print(wordcount)
add a comment |
lower all the words when comparing.for word.lower() in file.read().split():
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
convert it to stringfor str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
Not helping....
– U9-Forward
Nov 12 at 6:11
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
1
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
|
show 2 more comments
You can convert the words to lowercase, and then count them. So, your code changes to something like this.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
newWord = word.lower()
if newWord not in wordcount:
wordcount[newWord]=1
else:
wordcount[newWord]+=1
print(wordcount)
Basically, you will be storing in the dict, where keys are the lower case versions of each word.
Do note, that you will lose "data", if you are doing operations which are case sensitive.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256644%2fword-frequency-counter-in-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Seems like in the question your saying there is a uppercase and lowercase issue, so why not:
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word.lower() not in wordcount:
wordcount[word.lower()]=1
else:
wordcount[word.lower()]+=1
print(wordcount)
Or:
file=open("phrases.txt","r")
wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
for word in file.read().split():
wordcount[word.lower()]+=1
print(wordcount)
add a comment |
Seems like in the question your saying there is a uppercase and lowercase issue, so why not:
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word.lower() not in wordcount:
wordcount[word.lower()]=1
else:
wordcount[word.lower()]+=1
print(wordcount)
Or:
file=open("phrases.txt","r")
wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
for word in file.read().split():
wordcount[word.lower()]+=1
print(wordcount)
add a comment |
Seems like in the question your saying there is a uppercase and lowercase issue, so why not:
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word.lower() not in wordcount:
wordcount[word.lower()]=1
else:
wordcount[word.lower()]+=1
print(wordcount)
Or:
file=open("phrases.txt","r")
wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
for word in file.read().split():
wordcount[word.lower()]+=1
print(wordcount)
Seems like in the question your saying there is a uppercase and lowercase issue, so why not:
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
if word.lower() not in wordcount:
wordcount[word.lower()]=1
else:
wordcount[word.lower()]+=1
print(wordcount)
Or:
file=open("phrases.txt","r")
wordCount=.fromkeys([i.lower() for i in file.read().split()],1)
for word in file.read().split():
wordcount[word.lower()]+=1
print(wordcount)
answered Nov 12 at 6:07
U9-Forward
11.8k21136
11.8k21136
add a comment |
add a comment |
You can use an inbuilt function called Counter
for this as an alternative to looping through the list.
example :
from collections import Counter
file = open("phrases.txt","r")
data = file.read().lower().split() # added lower() will convert everything to lower case
wordcount = dict(Counter(data))
print(wordcount)
add a comment |
You can use an inbuilt function called Counter
for this as an alternative to looping through the list.
example :
from collections import Counter
file = open("phrases.txt","r")
data = file.read().lower().split() # added lower() will convert everything to lower case
wordcount = dict(Counter(data))
print(wordcount)
add a comment |
You can use an inbuilt function called Counter
for this as an alternative to looping through the list.
example :
from collections import Counter
file = open("phrases.txt","r")
data = file.read().lower().split() # added lower() will convert everything to lower case
wordcount = dict(Counter(data))
print(wordcount)
You can use an inbuilt function called Counter
for this as an alternative to looping through the list.
example :
from collections import Counter
file = open("phrases.txt","r")
data = file.read().lower().split() # added lower() will convert everything to lower case
wordcount = dict(Counter(data))
print(wordcount)
answered Nov 12 at 6:25
LonelyCpp
366111
366111
add a comment |
add a comment |
lower all the words when comparing.for word.lower() in file.read().split():
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
convert it to stringfor str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
Not helping....
– U9-Forward
Nov 12 at 6:11
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
1
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
|
show 2 more comments
lower all the words when comparing.for word.lower() in file.read().split():
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
convert it to stringfor str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
Not helping....
– U9-Forward
Nov 12 at 6:11
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
1
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
|
show 2 more comments
lower all the words when comparing.for word.lower() in file.read().split():
lower all the words when comparing.for word.lower() in file.read().split():
answered Nov 12 at 6:08
AmilaMGunawardana
645
645
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
convert it to stringfor str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
Not helping....
– U9-Forward
Nov 12 at 6:11
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
1
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
|
show 2 more comments
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
convert it to stringfor str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
Not helping....
– U9-Forward
Nov 12 at 6:11
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
1
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
That's not valid!!!, bad syntax
– U9-Forward
Nov 12 at 6:09
convert it to string
for str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
convert it to string
for str(word).lower() in file.read().split():
– AmilaMGunawardana
Nov 12 at 6:10
Not helping....
– U9-Forward
Nov 12 at 6:11
Not helping....
– U9-Forward
Nov 12 at 6:11
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
file=open("phrases.txt","r") wordCount= for str(word).lower() in file.read().split(): if word not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 6:15
1
1
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
file=open("phrases.txt","r") wordcount= for word in file.read().split(): weordtemp=word.lower() if weordtemp not in wordcount: wordcount[word]=1 else: wordcount[word]+=1 print(wordcount)
– AmilaMGunawardana
Nov 12 at 8:23
|
show 2 more comments
You can convert the words to lowercase, and then count them. So, your code changes to something like this.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
newWord = word.lower()
if newWord not in wordcount:
wordcount[newWord]=1
else:
wordcount[newWord]+=1
print(wordcount)
Basically, you will be storing in the dict, where keys are the lower case versions of each word.
Do note, that you will lose "data", if you are doing operations which are case sensitive.
add a comment |
You can convert the words to lowercase, and then count them. So, your code changes to something like this.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
newWord = word.lower()
if newWord not in wordcount:
wordcount[newWord]=1
else:
wordcount[newWord]+=1
print(wordcount)
Basically, you will be storing in the dict, where keys are the lower case versions of each word.
Do note, that you will lose "data", if you are doing operations which are case sensitive.
add a comment |
You can convert the words to lowercase, and then count them. So, your code changes to something like this.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
newWord = word.lower()
if newWord not in wordcount:
wordcount[newWord]=1
else:
wordcount[newWord]+=1
print(wordcount)
Basically, you will be storing in the dict, where keys are the lower case versions of each word.
Do note, that you will lose "data", if you are doing operations which are case sensitive.
You can convert the words to lowercase, and then count them. So, your code changes to something like this.
file=open("phrases.txt","r")
wordCount=
for word in file.read().split():
newWord = word.lower()
if newWord not in wordcount:
wordcount[newWord]=1
else:
wordcount[newWord]+=1
print(wordcount)
Basically, you will be storing in the dict, where keys are the lower case versions of each word.
Do note, that you will lose "data", if you are doing operations which are case sensitive.
answered Nov 12 at 6:10
MaJoR
408111
408111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256644%2fword-frequency-counter-in-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown