python - TypeError: write() 参数必须是 str,而不是 dict (Python)
问题描述
我正在开发一个在 txt 文件中显示我的信息的项目,它与 print 完美配合,但是当我尝试使用 write 时,它给了我以下错误:
TypeError: write() 参数必须是 str,而不是 dict
这是我的代码:
for x in range(len(list)):
outfile = open('message.txt', 'w')
outfile.write(list[x])
outfile.close()
解决方案
您不能将字典写入字符串。使用print(file=file_object)
或file.write(str(...))
。
代码:
for x in range(len(input_list)):
outfile = open('message.txt', 'w')
print(input_list[x], file=outfile)
outfile.close()
推荐阅读
- c++ - 如何使用 C++ 11 样式的非默认构造分配器指定初始大小?
- angularjs - Angularjs - 数组项依赖属性未更新
- python - Python为文件夹中的所有人创建多个zip文件
- oracle - 带有 Delphi DLL 的 Oracle 外部过程
- c# - Microsoft.Spark 中 Take 方法的方法未实现异常
- c# - C# MongoDB:在 $unwind 之后在数组上查询 $match
- sql - 如何获取 DolphinDB 中每台设备的最新记录?
- javascript - 在 Js 网格中更改页面时保存记录
- nlp - 从网络研讨会字幕训练 word2vec
- java - ORB 描述符如何找到相似的图像?