Write() Takes 2 Positional Arguments But 3 Were Given
My program produces the desired results correctly as I print them on the screen using the print() function: for k in main_dic.keys(): s = 0 print ('stem:', k) print ('w
Solution 1:
print()
takes separate arguments, file.write()
does not. You can reuse print()
to
write to your file instead:
withopen('result.txt', 'a', encoding='utf-8') as outf:
for k in main_dic:
s = 0print('stem:', k, file=outf)
print('word forms and frequencies:', file=outf)
for w in main_dic[k]:
print('%-10s ==> %10d' % (w,word_forms[w]), file=outf)
s += word_forms[w]
print ('stem total frequency:', s, file=outf)
print ('------------------------------')
I also used the built-in open()
, there is no need to use the older and far less versatile codecs.open()
in Python 3. You don't need to call .keys()
either, looping over the dictionary directly also works.
Solution 2:
file.write
is given multiple arguments when its expecting only one string argument
file.write('stem total frequency:', s)
^
The error gets raised as 'stem total frequency:', s
are treated as two different arguments. This can be fixed by concatenation
file.write('stem total frequency: '+str(s))
^
Solution 3:
file.write('stem:', k)
You're supplying two arguments to write
on this line, when it only wants one. In contrast, print
is happy to take as many arguments as you care to give it. Try:
file.write('stem: ' + str(k))
Post a Comment for "Write() Takes 2 Positional Arguments But 3 Were Given"