簡體   English   中英

在Python中某些字符后獲取子字符串

[英]Getting Substring after certain char in python

我想剪切諸如“ 0011165.jpg_Fish”之類的字符串以僅獲取Fish,因此“ _”之后的所有內容,我該如何在python中做到這一點?

非常感謝你!

請使用str.partition而不是str.split 這是可靠的,因為您總是可以預期3項目,與split不同,如果input string不包含split character ,可能很難處理。

>>> word = '0011165.jpg_Fish'
>>> not_required, split_char, required = word.partition('_')
>>> required
'Fish'

嘗試

"0011165.jpg_Fish".split("_")[1]

如果是數據框

train['Label'] = train.Image_Labels.str.split("_").str[1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM