[英]How to print strings from a text file,separated by new line in Python
[英]a line contains strings,separated by ','
在Python中,嘗試執行以下操作:我有一行包含由','分隔的幾個字符串。 我想刪除其中具有特定子字符串的每個字符串。 例如:
line: first_name,second_name,every_single_name_exicted,dog,cat.
我想刪除其中包含單詞“ name” +“'”的每個字符串,因此結果將是:
line: dog,cat.
並不是:
line: ,,,dog,cat
我可以用什么來實現這一目標? 以及當我不在乎某個子字符串的左側或右側時,如何一般搜索該子字符串(例如,當您可以輸入name時,例如NotePad中的搜索選項)
s = 'first_name,second_name,every_single_name_exicted,dog,cat'
s = s.split(',')
a = ','.join([i for i in s if 'name' not in i])
print(a)
嘗試這個
if not in
按“名稱”過濾。 像這樣:
mystring = "first_name, second_name, every_single_name_exicted,dog,cat"
newstring = ", ".join(s.strip() for s in mystring.split(",") if not "name" in s) + "."
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.