python - 如何从字符串末尾检测和删除尾随反斜杠?
问题描述
任何文件的条目都有一个尾随反斜杠
cache[path] = file.read()
for file_name, multi_line_content in cache.items():
for line in multi_line_content.splitlines():
// this line may or maynot have trailing backslash
前任:
dsf dff dfdf \
cvcs dsd sdds\
在将完整行复制为字符串之前,如何处理此行,检测尾随反斜杠并将其删除?
解决方案
rstrip
正是您所需要的:
s = s.rstrip('\\')
推荐阅读
- python - 定期更新带有标签的 ProgressBar
- sql-server - 执行 tsqlt 测试时 tsqlt 中的回滚错误
- python - 带有管道python的子进程
- swift - 在 Swift 应用程序中控制动态文本
- ansible - Ansible 使用 {{ item }} 和 csvfile 查找
- sql - 删除大量记录导致数据库锁定
- java - 当缓存已满时,在将新实体添加到缓存之前,休眠会从缓存中逐出哪个现有实体?
- java - 如何配置 RabbitMQ 以平等地为多个消费者服务多个队列
- r - 使用 mutate (ifelse()) 创建新变量,但由于 NA 而得到错误答案
- powerbi - 如何在 Power BI 中按组计算单独的平均值?