我有一个SQL文件,我已将其转换为txt文件。
最后的想法是用Python3做一个readline()并找到一个特定的字符串,并只返回有这个字符串的行。
例如,想象一下这个file.txt:
*hello, how are you
I am fine, --- thanks*/
What a wonderful day
Yes, it ---is
Regards---*
我只需要打印(或指定变量/变量)字符串“---”出现的行:
*I am fine, --- thanks*/
Yes, it ---is
Regards---*
提前致谢。
答案 0 :(得分:1)
使用列表理解:
print ([line for line in open("test.txt").readlines() if "---" in line])
如果你想要输出在不带" \ n"的单独行中而不是列表:
my_list = [line.strip() for line in open("test.txt").readlines() if "---" in line]
for line in my_list:
print(line)
line.strip()将删除" \ n"。