My program produces the desired results correctly as I print them on the screen using the print()
function:
当我使用print()函数在屏幕上打印时,程序会正确地生成所需的结果:
for k in main_dic.keys():
s = 0
print ('stem:', k)
print ('word forms and frequencies:')
for w in main_dic[k]:
print ('%-10s ==> %10d' % (w,word_forms[w]))
s += word_forms[w]
print ('stem total frequency:', s)
print ('------------------------------')
I want to write the results with the exact format to a text file though. I tried with this:
我想把结果用精确的格式写入文本文件。我试着用这个:
file = codecs.open('result.txt','a','utf-8')
for k in main_dic.keys():
file.write('stem:', k)
file.write('\n')
file.write('word forms and frequencies:\n')
for w in main_dic[k]:
file.write('%-10s ==> %10d' % (w,word_forms[w]))
file.write('\n')
s += word_forms[w]
file.write('stem total frequency:', s)
file.write('\n')
file.write('------------------------------\n')
file.close()
but I get the error:
但是我得到了错误:
TypeError: write() takes 2 positional arguments but 3 were given
TypeError: write()接受两个位置参数,但是给出了3个。
3 个解决方案
#1
3
print()
takes separate arguments, file.write()
does not. You can reuse print()
to write to your file instead:
print()使用单独的参数,file.write()不。您可以重复使用print()来将其写到文件中:
with open('result.txt', 'a', encoding='utf-8') as outf:
for k in main_dic:
s = 0
print('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.
我还使用了内置的open(),不需要在Python 3中使用更老、更少用途的codecs.open()。您不需要调用.keys(),也可以直接对字典进行循环。
#2
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
错误被提高为“stem总频率:”,s被视为两个不同的参数。这可以通过连接来确定。
file.write('stem total frequency: '+str(s))
^
#3
1
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))
#1
3
print()
takes separate arguments, file.write()
does not. You can reuse print()
to write to your file instead:
print()使用单独的参数,file.write()不。您可以重复使用print()来将其写到文件中:
with open('result.txt', 'a', encoding='utf-8') as outf:
for k in main_dic:
s = 0
print('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.
我还使用了内置的open(),不需要在Python 3中使用更老、更少用途的codecs.open()。您不需要调用.keys(),也可以直接对字典进行循环。
#2
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
错误被提高为“stem总频率:”,s被视为两个不同的参数。这可以通过连接来确定。
file.write('stem total frequency: '+str(s))
^
#3
1
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))