简体   繁体   English

如何删除/替换字符串中第n个特殊字符之前和之后的字符串?

[英]How to remove/replace strings before and after nth special character in a string?

如何使用Python Replace方法删除第三个_及其之前的所有字符串以及第四个_及其之后的所有字符串

st = "the_gis_osm_natural_a_free_1.shp"

You could split the string by _ and then join the result: 您可以用_ 分割字符串,然后加入结果:

start, end = 3, 4
filename = "the_gis_osm_natural_a_free_1.shp"
print('_'.join(filename.split("_")[start:end]))

Output 输出量

natural

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM