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
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 996 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Search in a file using regular expressions ADELE80 2 662 Dec-18-2024, 12:29 PM
Last Post: ADELE80
  Numpy, array(2d,bool), flipping regions. MvGulik 2 952 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,025 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  ValueError: could not broadcast input array from shape makingwithheld 1 2,176 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  python code to calculate mean of an array of numbers using numpy viren 3 1,115 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 751 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,193 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 2,521 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 1,305 Mar-21-2024, 08:08 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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