Mar-13-2018, 02:25 PM
(This post was last modified: Mar-13-2018, 02:55 PM by Mr_Keystrokes.)
I have an array (list), how can I search a separate file for the presence of elements that are in my array?
I wanted to use python to do it instead of bash because python is where it's at these days, right?
I have collected a set of strings in my array.
I am now trying to search a file for the presence or absence of my strings in the file.
`File "/home/miniconda3/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object`
Does occur because array elements are bytes that cannot be compared to strings?
Previously I had this:
I wanted to use python to do it instead of bash because python is where it's at these days, right?
I have collected a set of strings in my array.
I am now trying to search a file for the presence or absence of my strings in the file.
#!/usr/bin/python import re with open("/home/all_genera.txt") as file: generaA=[] for line in file: line=line.strip('\n') generaA.append(line) joiner='|' .join(generaA) re.findall(joiner, open("/home/config/config2.cnf", 'r'), re.M ).read()When I run this script the following is returned:
`File "/home/miniconda3/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object`
Does occur because array elements are bytes that cannot be compared to strings?
Previously I had this:
#!/usr/bin/python with open("/home/all_genera.txt") as file: generaA=[] for line in file: line=line.strip('\n') generaA.append(line) #print (generaA) with open("/home/config2.cnf") as config_file: counter = 0 for line in config_file: line=line.strip('\n') for part in line .split(): if generaA[counter]in part: print (generaA[counter], "is -----> PRESENT") else: print (generaA[counter], "is ABSENT") counter += 1But this doesn't cycle through my entire array on each line, instead it checks whether only the first element is in any of the lines of the file.