Python:从fi的打印中排除某些文本

2024-09-26 17:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为“列表”的文件,我需要打印某些信息,但不是整个文件。我的档案如下:

pool
pool/return
pool/home
pool/home/audits
pool/home/audits/2013
pool/home/audits/2014
pool/home/exchanges
pool/home/term
pool/transfers

我需要打印“/home/”下的所有内容,除了“audits/”部分。下面是我的代码:

^{pr2}$

我得到以下信息:

pool
pool/return
pool/home
pool/home/exchanges
pool/home/term
pool/transfers

但我只需要打印:

pool/home/exchanges
pool/home/term

Tags: 文件代码信息内容home列表return档案
1条回答
网友
1楼 · 发布于 2024-09-26 17:38:38

您必须检查“/home”是否一致,以及“home/audits”是否一致:

if '/home' in line and not 'home/audits' in line:

条件if '/home' and not 'home/audits' in line:

get已转换为if True and not 'home/audits' in line:

因为只要填充字符串,bool('/home')(或bool('any string'))始终为真

相关问题 更多 >

    热门问题