0
with open("words_10percent.txt") as f:
   file_data = f.read()
word_frequency = {}
for line in file_data.split("\n"):
   word, frequency = line.split(",")
   word_frequency[word] = float(frequency)

In line 5 I get a value error when trying to split the line.

Adam Fellows
  • 21
  • 1
  • 1

3 Answers3

1

Your error is this line: word, frequency = line.split(",") Some line doesn't have " , "

try this:

with open("words_10percent.txt") as f:
   file_data = f.read()
word_frequency = {}
for line in file_data.split("\n"):
   if ',' not in line:
       print('line without ,:', line)
       continue
   word, frequency = line.split(",")
   word_frequency[word] = float(frequency)
1

This code is absolutely correct. There is no error. Check your text file again.

for demo purpose, I am adding four lines in 'words_10percent.txt' file as follows:

Ram, 50.55
class, 45.88
black, 35.99
data, 35.60

and I am getting the following output :

Ram
 50.55
class
 45.88
black 
 35.99
data
 35.60
Anshul Vyas
  • 633
  • 9
  • 19
0

your issue is generated by your data, you do not have in at least one line the character ,

you may check your last line, probably has a new line character \n

kederrac
  • 16,819
  • 6
  • 32
  • 55