python - Python Strip 不删除字符
问题描述
我正在尝试从照片 URL 中删除一些不需要的字符。
相关代码:
for img in imgSrc:
print(img)
img.strip('US40')
print(img)
没有抛出错误,但两个打印语句的输出相同:
https://images-na.ssl-images-amazon.com/images/I/51uUvUZNoUL._AC_US40_.jpg
https://images-na.ssl-images-amazon.com/images/I/51uUvUZNoUL._AC_US40_.jpg
https://images-na.ssl-images-amazon.com/images/I/51GQJaFyk1L._AC_US40_.jpg
https://images-na.ssl-images-amazon.com/images/I/51GQJaFyk1L._AC_US40_.jpg
解决方案
strip 仅适用于前导和尾随字符。在这种情况下,您应该使用替换
剥离: S.strip([chars]) -> str
返回删除了前导和尾随空格的字符串 S 的副本。如果给出了 chars 而不是 None,则改为删除 chars 中的字符。
替换: S.replace(old, new[, count]) -> str
返回 S 的副本,其中所有出现的子字符串 old 都替换为 new。如果给定了可选参数 count,则仅替换第一个 count 出现。
for img in imgSrc:
print(img)
img = img.replace('US40',"")
print(img)
推荐阅读
- amazon-web-services - 在 aws elasticsearch 中插入 json 文件时出错
- excel - 公式 IF 范围之间的时间
- java - 从字节数组序列中读取行
- javascript - 如何修复 data.map 不是函数 api
- kubernetes - 删除命名空间后,pod 具有未绑定的立即持久性卷声明
- html - 如何将框阴影添加到 svg 路径圆形
- angular - Angular 8:(Angular 材质选项卡)如何使用 prev 和 next 箭头在多个选项卡之间切换?
- flutter - 如何在 Flutter 中更改 DataTable 的列宽?
- ruby-on-rails - 使用 Ralyxa 实现向上/向下滚动功能
- java - Java 8 提供函数式风格但不是函数式编程的说法是否属实?