python编写一个程序,将txt文件每行开头的tshe/dadwq/删掉
时间: 2024-05-12 12:12:48 浏览: 19
可以使用Python中的字符串操作来删除每行开头的指定字符串,然后将文本文件重新写入。
以下是一个示例代码:
```python
with open('input.txt', 'r') as file:
lines = file.readlines()
new_lines = []
for line in lines:
if line.startswith('tshe/'):
new_lines.append(line[5:])
elif line.startswith('dadwq/'):
new_lines.append(line[6:])
else:
new_lines.append(line)
with open('output.txt', 'w') as file:
file.writelines(new_lines)
```
这里假设输入文件名为'input.txt',输出文件名为'output.txt'。程序将读取输入文件的所有行,然后删除每行开头的'tshe/'或'dadwq/',并将修改后的行写入输出文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)