【问题标题】:How can I filter out a List from a .txt file using list comprehension?如何使用列表理解从 .txt 文件中过滤出列表?
【发布时间】:2020-11-12 03:33:55
【问题描述】:

我正在学习 Python 课程,但我不知道带回家的测验。我正在使用 IDLE 编写代码。

我们必须将名为 names.txt 的文件加载到列表中。该文件包含以下内容:

Joe Smith
000000
Jeff Mitchell
xxxxxxx
Benjamin Grant
12346

我需要过滤掉包含“xxxxxxx”或数字的行。我正在尝试将列表理解与以下代码一起使用:

> names = open(r'C:\Users\eesqu\Documents\Python\names1.txt','r')
> names_contents = names.read()
> filtered_names = [n for n in names_contents if n !='xxxxxxx']
> names.close()
> print(filtered_names)

但是,当我打印过滤后的名称输出时,名称并没有被过滤,而是以下拉格式显示,它们看起来像这样:

['J', 'o', 'e', '', 'S', 'm', 'i', 't', 'h', '\n', '0', '0 '、'0'、'0'、'0'、'0'、'\n'、'J'、'e'、'f'、'f'、''、'M'、'i'、 't'、'c'、'h'、'e'、'l'、'l'、'\n'、'x'、'x'、'x'、'x'、'x'、' x','x','\n','B','e','n','j','a','m','i','n','','G' , 'r', 'a', 'n', 't', '\n', '1', '2', '3', '4', '6', '\n']

我在这里做错了什么?是否可以同时过滤掉“xxxxxxx”和数字?

感谢您在我开始编写代码时的支持。

【问题讨论】:

  • 您需要打开文件并逐行读取。 pythonforbeginners.com/files/…
  • @Erick Esquivel 如果以下答案之一满足您的问题,请接受它,这样这个问题就可以结束了

标签: python python-3.x list list-comprehension


【解决方案1】:

你快到了

names = open(r'C:\Users\eesqu\Documents\Python\names1.txt','r')
name_contents = names.readlines()  # list of lines
filtered_names = [n for n in name_contents if (not n.isnumeric() or n != 'xxxxxxx']

在此处发布之前,您可能希望使用您最喜欢的搜索引擎查找内容。这是一个非常琐碎的问题。

【讨论】:

  • 谢谢,这很有帮助。我还在纠结在哪里可以找到最好的资源,并且很难在 Google 上找到有用的东西。我很想听听有关如何更好地利用这个论坛的任何提示。
  • 例如我查了“python 逐行读取文件”,前几个结果都向读者介绍了readlines()。根据我的经验,使用较少关键字的简单搜索查询比更冗长的搜索查询效果更好。
【解决方案2】:

您可以使用readlines读取数据和列表推导过滤掉xxx

ss = '''
Joe Smith
000000
Jeff Mitchell
xxxxxxx
Benjamin Grant
12346
'''.strip()

with open('names.txt','w') as f: f.write(ss)  # write data file

###############################


with open('names.txt') as f:
   lns = f.readlines()
   xx = [ln.strip() for ln in lns if ln.strip() != 'xxxxxxx']
   
print('\n'.join(xx))

输出

Joe Smith
000000
Jeff Mitchell
Benjamin Grant
12346

【讨论】:

    【解决方案3】:

    names_contents 是一个字符串,因此您在这行代码n !='xxxxxxx' 中将字符串与字符进行比较。因此,首先您必须将字符串拆分为代表每一行的字符串列表。试试这个

    lines = names_contents.split("\n")
    filtered_names = [n for n in lines if n !='xxxxxxx']
    

    【讨论】:

      【解决方案4】:

      您要删除的值 filter_vals = 'xxxxxxx\n'

      读取文件

      with open('64797525.txt') as f:
          out = [i.strip() for i in f.readlines() if i not in filter_vals] # remove what's in the list
      
      print(out)
      ['Joe Smith', '000000', 'Jeff Mitchell', 'Benjamin Grant', '12346']
      

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 2022-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-28
        相关资源
        最近更新 更多