python - 如何从python中的文件中删除符号?
问题描述
我很难"//!"
从我试图用 python 解析的文件的句子开头删除它。
with open("dwe.txt", "r") as file1:
for row in file1:
print(row.rstrip('//!'))
预期产出
The flag should not process everything that was given at the time
it was processing.
实际输出
//! The flag should not process everything that was given at the time
//! it was processing.
解决方案
正如@Kevin 提到的,rstrip()
,lstrip()
并strip()
删除包含字符串的所有变体,直到它遇到一个不匹配的字符,所以它不适合您的操作。例如:
>>> 'barmitzvah'.lstrip('bar')
'mitzvah'
>>> 'rabbit'.lstrip('bar')
'it'
>>>'rabbarabbadoo'.lstrip('bar')
'doo'
尝试startswith()
改用:
with open("dwe.txt", "r") as file1:
for row in file1.readlines():
if row.startswith('//! '):
print(row[3:])
推荐阅读
- tensorflow - 使用张量作为网络中的索引
- r - 如何将 chr 转换为其他内容并将其分成 4 个单元?
- python - 使用 Python Paramiko 将所有文件从本地目录上传到 SFTP 服务器
- python - Plotly:将特定值添加到默认刻度
- excel - 获取最后一个停用的工作表
- c# - C# 记录类型:如何本地化自动生成的 ToString 方法
- javascript - 使用withformik,react和typescript再次关闭和打开面板后如何在输入div中保留搜索字符串?
- microsoft-graph-api - 创建“是/否”列时是否可以设置默认值?
- python - 如何制作一个 tkinter 窗口在被破坏后将停止自我更新?
- c# - 删除 Visual Studio 2019 上的 Source Map 部分