首页 > 解决方案 > 如何从字符串末尾检测和删除尾随反斜杠?

问题描述

任何文件的条目都有一个尾随反斜杠

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\


在将完整行复制为字符串之前,如何处理此行,检测尾随反斜杠并将其删除?

标签: pythonstring

解决方案


rstrip正是您所需要的:

s = s.rstrip('\\')

推荐阅读