Python Forum
I have an array, how can I search a seperate file for the contents of my array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have an array, how can I search a seperate file for the contents of my array?
#1
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.

#!/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 += 1
But 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 5 139 15 minutes ago
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 191 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,723 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 305 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 526 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 980 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 508 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,257 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  Convert np Array A to networkx G IanAnderson 2 628 Jul-05-2023, 11:42 AM
Last Post: IanAnderson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020