如何从txt.readlines()中提取数据? [英] How to extract data from a txt.readlines()?
本文介绍了如何从txt.readlines()中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个似乎读取文件的小问题的代码...但是问题是它无法识别单词/字母,这就是我的意思. extract('test.txt',('A','E','I','O','U'))
test.txt
是txt文件,而('A','E','I','O','U')
是我要查找的单词.
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.
我的代码:
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'))
我的输出是什么
NO
NO
NO
NO
Yay
这是预期的输出:
Yay
Yay
Yay
Yay
Yay
内部 test.txt
:
A
E
I
O
U
推荐答案
我将在此保留.如果最终结果是将 find
内的每个变量与 file
中的每一行进行比较,那么这应该可以完成:
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:
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'))
这篇关于如何从txt.readlines()中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文