python - 如何将我的“打印内容”添加到文本文件中
问题描述
最近开始学习Python。
我定义了标题和价格。
现在我希望 Python 创建一个文本文件,并将“标题”和“价格”的内容输入到新创建的文本文件中
print(title)
print(price)
fh = open("Price&Title", "w")
fh.write()
fh.close()
上面的这段代码不起作用。
解决方案
您可以使用以下file
参数print
:
fh = open("Price&Title", "w")
print(title, file=fh)
print(price, file=fh)
fh.close()
推荐阅读
- f# - Async.Parallel 不运行列表 |> Async.Parallel |> Async.Ignore |> Async.Start?
- excel - 如何编写一个在列中定位特定值的循环,如果找到一个,将公式写入相邻单元格,然后继续
- android - 在 apk 安装期间读取 xml 文件及其内容
- rust - Rocket.rs:可选的 PathBuf 没有匹配的路由
- python - 带循环的条件列
- java - 为什么这个复合主键被视为唯一的,我该如何解决?
- python - Excel IF 到 python
- r - 如何将R中的pdf图作为恒定图像?
- node.js - TSOA Express App 返回无法获取端点
- robo3t - 如何使用 Robo 3T 连接到 AWS Documentdb?