Reputation: 11
I have a question to make an "output.txt". I would like to write both word and prob(l.19) results into an "output.txt" file. When I write "model_file.write(word, prob)", the terminal scolds me with "TypeError: function takes exactly 1 argument (2 given)" message. I tried to add more arguments but it didn't work.. Could anybody help me with my question??
THIS IS A WORD COUNT.PYtotal_count = 0
train_file = open(sys.argv[1],"r")
for line in train_file:
words = line.strip().split(" ")
words.append("</s>")
for word in words:t
counts[word] = counts.get(word, 0) + 1
total_count = total_count + 1
model_file = open('output.txt',"w")
for word, count in sorted(counts.items(),reverse=True):
prob = counts[word]*1.0/total_count
print "%s --> %f" % (word, prob)
model_file.write(word, prob)
model_file.close()
#
Upvotes: 1
Views: 15237
Reputation: 502
I wanna to created a kind on description about my df so I write this:
# Create an empty txt
f = open(os.path.join(pathlib.Path().absolute(),'folder','florder','name.txt'), "a")
# Create an kind of header
f.write('text'+'\n')
f.write('text'+'\n')
f.write("""
-------------------
""")
f.write('text:'+ '\n')
f.write("""
""")
for c in range(0, len(df.columns)):
campo = df.columns[c]
if df[df.columns[c]].dtype== 'object':
text= 'Tex'
outfile = open('name.txt','w')
f.write('str:'+"'"+str(xxx)+"'"'\n')
f.write('str:'+ str(str)+'\n')
f.write('\n')
f.close()
Upvotes: 0
Reputation: 4487
Just simply replace
model_file.write(word, prob)
with
model_file.write(word+' '+str(prob)+'\n')
Be aware that the method write()
was implemented to take only one string argument, so you have to convert prob
into a string (by the method str()
) and then combine it with word
by the string operator +
, so that you got only one string argument.
P.S.: though you didn't ask this, I have to say that if you are going to write each word and its probability, you should put model_file.write(word+' '+str(prob)+'\n')
into the for
statement. Otherwise, if you resist to call it outside of the for
statement for some purpose, then you should assign word
and prob
outside of the for
statement too. Or it would cause another error.
Upvotes: 3
Reputation: 993183
You could use the print
statement to do this:
print >>model_file, word, prob
Upvotes: 1