python - 从另一个文件替换字符串和文件
问题描述
我有两个文本文件, man.txt 和 job.txt
在 man.txt 我有
abc = "pqr"
abc1 = "xyz"
在 job.txt 我有
pqrst
xyz
我想在 Python 或 Unix中查找并替换"pqr"
man.txt"pqrst"
我通过 abc 知道 pqr 是要替换的(abc 是特定的)
提前致谢
解决方案
您可以使用fileinput
.
import fileinput
with fileinput.FileInput("man.txt", inplace=True) as file:
for line in file:
print(line.replace('abc = "pqr"', 'abc = "pqrst"'), end='')
推荐阅读
- visual-studio-code - 删除对特定易受攻击包的依赖
- css - 指定大量行的简单方法?
- django - Django - 为 urls.py 中的特定 url 禁用身份验证器?
- mysql - 我们可以从 My SQl 中的 Common Table Expression 中删除列或更改列顺序吗?
- python - 在哪里查看打印语句或查找 Docker 容器中生成的输出?
- python - 二项式分区的差异
- android - 我在我的安卓模拟器上看不到我的 ParsePush 通知
- python - 如何在 Python 中为这个线性规划问题建模?
- node.js - 如何使用nodejs访问for循环外部的变量值并在对话框流中显示给用户
- angular - 显示带有角度的随机图像