首页 > 解决方案 > 为什么在这种情况下,python 的 strip 和 lstrip 方法的行为不符合预期?

问题描述

我正在尝试从凌乱的 CSV 字符串中删除一些垃圾,但我不明白为什么 strip 和 lstrip 的行为不符合预期。

我希望这会返回“ABC”,但似乎无法删除这些字符。

>>> a = '","ABC"'
>>> a
'","ABC"'
>>> a.strip('",')
'ABC'
>>> a.lstrip('",')
'ABC"'

标签: pythonpython-3.xstrip

解决方案


  • a.lstrip('",')删除所有','s 和'"'s 开头的 s a。(条。)
  • a.rstrip('",')删除所有','s 和'"'s 末尾的 s a。(右侧条带。)
  • a.strip('",')删除s两侧的所有','s 和s 。'"'a

推荐阅读