python - 为什么在这种情况下,python 的 strip 和 lstrip 方法的行为不符合预期?
问题描述
我正在尝试从凌乱的 CSV 字符串中删除一些垃圾,但我不明白为什么 strip 和 lstrip 的行为不符合预期。
我希望这会返回“ABC”,但似乎无法删除这些字符。
>>> a = '","ABC"'
>>> a
'","ABC"'
>>> a.strip('",')
'ABC'
>>> a.lstrip('",')
'ABC"'
解决方案
a.lstrip('",')
删除所有','
s 和'"'
s 开头的 sa
。(左条。)a.rstrip('",')
删除所有','
s 和'"'
s 末尾的 sa
。(右侧条带。)a.strip('",')
删除s两侧的所有','
s 和s 。'"'
a
推荐阅读
- git - 如何从 GitHub 获取分叉代码文件到我的本地计算机?
- pandas - 对于每一行,我想计算数据框中所有 Y 列中重复的特定变量的频率
- python-3.x - python __eq__ 测试失败
- python - 我的程序告诉某个变量未定义,我该如何解决?
- sql - 创建表时使用 NULL 约束的原因?
- python - Py serial readline在Python3中不起作用
- javascript - 有没有办法在不重新加载整个应用程序的情况下对节点应用程序进行更改?
- numbers - 编写一个 C++ 程序将十进制转换为任意基数
- python - 在 Python 中解包字节值
- data-structures - 对可变结构字段中的值调用可变方法