1

「output.txt」の作り方について質問があります。word と prob(l.19) の両方の結果を「output.txt」ファイルに書き込みたいと思います。「model_file.write(word, prob)」と書くと、端末は「TypeError: 関数は正確に 1 つの引数をとります (2 つ与えられます)」というメッセージで私を叱ります。さらに引数を追加しようとしましたが、うまくいきませんでした..誰か私の質問を手伝ってくれませんか??

これは単語 COUNT.PY です
total_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()
#
4

3 に答える 3

1

printこれを行うには、次のステートメントを使用できます。

print >>model_file, word, prob
于 2012-07-01T07:34:00.927 に答える
0

私は自分の df についての種類の説明を作成したいので、これを書きます:

# 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()
于 2021-05-20T22:26:35.707 に答える