Python中文
首页
教程
问答
标签
搜索
登录
注册
python write可以接受2个参数
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有个问题要做一个“output.txt”。 我想把word和prob(l.19)结果都写进 一个“output.txt”文件。 当我写“model_file.write(word,prob)”时,终端会用 “TypeError:函数只接受1个参数(给定2个)”消息。 我试图增加更多的论点,但没有成功。。 有谁能帮我提个问题吗??</p> 这是单词COUNT.PY <pre><code>total_count = 0 train_file = open(sys.argv[1],"r") for line in train_file: words = line.strip().split(" ") words.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>("</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() </code></pre> #
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您可以使用<code>print</code>语句执行以下操作:</p> <pre><code>print >>model_file, word, prob </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何将python输出重定向到python控制台和Windows中的文本文件
6 回答
如何将Python运行时嵌入运行在Windows上的R包中
8 回答
如何将python进程作为另一个Windows us运行
3 回答
如何将Python进程的输出用Python管道传输?
5 回答
如何将Python进程的输出重定向到Rust进程?
5 回答
如何将python连接到Azure云并创建Azure数据工厂
7 回答
如何将Python连接到Db2
8 回答
如何将python连接到IBMDB2?
4 回答
如何将Python连接到microsoftaccess数据库文件?
2 回答
如何将python连接到MySQL服务器
2 回答
如何将Python连接到Node.js?
6 回答
如何将python连接到Oracle Application Express
5 回答
如何将Python连接到PostgreSQL
6 回答
如何将Python连接到Postgres服务器?
5 回答
如何将Python连接到SAS Enterprise Guide(EG)服务器
3 回答
如何将Python连接到Spark会话并保持RDDs的Ali
3 回答
如何将python连接到sqlite3并在上填充多行
1 回答
如何将python连接到使用docker运行的cassandra
8 回答
如何将python退格应用于字符串
7 回答
如何将python逻辑应用到tkinter GUI中?这是一个简单的GET请求程序
7 回答