简体   繁体   English

如何从 txt.readlines() 中提取数据?

[英]How to extract data from a txt.readlines()?

I have a code that seems to have a slight issue it reads the file... But the issue is it can not identify the words/letters this is what i mean extract('test.txt',('A','E','I','O','U')) the test.txt is a txt file and the ('A','E','I','O','U') is the words i want to find.我有一个代码似乎有一个小问题它读取文件...但问题是它无法识别单词/字母这就是我的意思extract('test.txt',('A','E','I','O','U')) test.txt是一个 txt 文件而('A','E','I','O','U')是单词 i想找到。

my code:我的代码:

def extract(file,find=()):
  with open(str(file),'r') as file:
    find = list(find)
    read_file = file.readlines()
    
    num1 = 0
    num = num1-1
    for vals in find:
      num1 += 1
    pass  
  
    for finds in read_file:
      linef = str(finds)
      main_obj = find[num]
        
      if main_obj in linef:
        print("Yay")
        
      elif linef != main_obj:
        print("NO")
        
      else:
          print("???")
    pass  


extract('test.txt',('A','E','I','O','U'))

What my output is:我的 output 是什么:

NO
NO
NO
NO
Yay

This is the expected output:这是预期的 output:

Yay
Yay
Yay
Yay
Yay

inside test.txt :test.txt里面:

A
E
I
O
U

I'll just leave this here.我就把这个留在这里。 If the end result is to compare each variable inside of find to each line in file then this should do the job:如果最终结果是将find中的每个变量与file中的每一行进行比较,那么这应该可以完成工作:

def extract(file, find=()):
    with open(file) as F:
        contents = F.readlines()

    for line in contents:
        if any([i in line for i in find]):
            print("Yay")
        else:
            print("NO")

extract("test.txt", ('A','E','I','O','U'))

In the code:在代码中:

for finds in read_file:
  linef = str(finds)
  main_obj = find[num]
    
  if main_obj in linef:
    print("Yay")
    
  elif linef != main_obj:
    print("NO")

you never increment num and hence are essentially always comparing find[-1] with main_obj .您永远不会增加num ,因此基本上总是将find[-1]main_obj进行比较。 I guess you have to increment num if there is a match or whatever your requirement is.我想如果有匹配项或任何您的要求,您必须增加num

if main_obj in linef:
  num += 1
  print("Yay")

Either way, this would be the most down-to-earth correct way to do what you are required to:无论哪种方式,这将是执行您需要执行的操作的最实际的正确方法:

for temp in find:
    not_found = True
    for finds in read_files:
        if temp in finds:
            not_found = False
            print("Yay")
            break
        elif temp != finds:
            not_found = True # elif is not needed TBH but just adding for clarity
    if not_found:
        print("NO")

You just need to update your logic and remove this code.您只需要更新您的逻辑并删除此代码。 because it will always return -1 index on every increment.因为它总是会在每次增量时返回 -1 索引。

num1 = 0
num = num1-1
for vals in find:
  num1 += 1
pass  

Just use one variable and increment it accordingly.只需使用一个变量并相应地增加它。

def extract(file, find=()):
    num = 0
    with open(str(file), 'r') as file:
        find = list(find)
        print(find)
        read_file = file.read().splitlines()
        print(read_file)

        for finds in read_file:
            linef = str(finds)
            main_obj = find[num]
            num += 1

            if main_obj in linef:
                print("Yay")

            elif linef != main_obj:
                print("NO")

            else:
                print("???")
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM