[英]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.