Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting text file to float and into dictionary to find frequency

I am given a text file that is all words with no punctuation, just spaces separating the words. My goal is find and print the word that has the highest frequency in the file along with its frequency. I am able to do this no problem with a similar problem that involves a text file with many numbers but cannot get this to work without getting an error.

This is my attempt at getting it to work. it works fine for the other text file using int() instead of float(). The two things that I think might work are using split() or using .append(). I am not quite sure how to properly integrate them into this. I have read previous posts that are similar, but either they do not involve using a dictionary to calculate frequency or they are not specifically words.


words = {}
myfile = open("HAPPY.txt")

for x in myfile:
    y = float(x.strip())
    if y not in words:
        words[y] = 1
    else:
        words[y] = words[y] + 1

for x in words:
    if words[x] == max(words.values()):
        print(x, words[x])

The error i receive is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Traceback (most recent call last):
  File "C:\Users\jekni\PycharmProjects\Python Class\Homework\HW 7.py", line 31, in <module>
    y = float(x.strip())
        ^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: 'Once upon a time in a faraway land there lived a man and a woman'

edit

sample of text file:
Once upon a time in a faraway land there lived a man and a woman
They lived in a humble cottage in the heart of the woods where they found peace and happiness in the simplicity of their life

sample of number file I mentioned earlier in post:
69
35
31
75
87
56
55
67
30

>Solution :

to convert the file to a string you need to use the read() method.
I changed your code and also add a split method so it make an array with all the words

words = {}
myfile = open(HAPPY.txt)

myfile = myfile.read().split()

for y in myfile:
    if y not in words:
        words[y] = 1
    else:
        words[y] += 1

for x in words:
    if words[x] == max(words.values()):
        print(x, words[x])

(I dont know how you file would look like so you may also have to split line jumps)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading