Linked Questions

Popular Questions

Open file into array, search for string and return value

Asked by At

Alright, I've been working on this for a while and cannot get it.

I'm making a method that accepts a filename, and a pattern.

E.g findPattern(fname, pat)

Then the goal is to look for that pattern, say the string "apple" within the text file that is opened, and return it's location by [line, beginning character index] I'm new to python and have been told numerous ways, but they are either too complicated or we aren't allowed to use them such as index; we are specifically supposed to use arrays.

My thoughts were two nested for loops, the outside goes through each index of the textfile array, and the inner for loop compares the first letter of the desired pattern. If found, the inner loop will inrement so now it's checking the p in apple vs the text file.

One major problem is I cannot get the file into an array, I've only been able to do an entire line.

Here's something I have, although doesn't quite work. I was just experimenting with .tell to show me where it's at but it's always at 141, which I believe is the EOF but I haven't checked.

#.....Id #
#.....Name

#########################
#my intent was for you to write HW3 code as iteration or
#nested iterations that explicitly index the character 
#string as an array; i.e, the Python index() also known as 
#string.index() function is not allowed for this homework.
########################

print
fname = raw_input('Enter filename: ')
pattern = raw_input('Enter pattern: ')

def findPattern(fname, pat):

    f = open(fname, "r")
    for line in f:
        if pat in line:
            print "Found it @ " +(str( f.tell()))
            break
    else:
        print "No esta..."    

print findPattern(fname, pattern)

EDIT:

fname = raw_input('Enter filename: ')
pattern = raw_input('Enter pattern: ')

def findPattern(fname, pat):

    arr = array.array('c', open(fname, 'rb').read())

    for i in xrange(len(arr)):
        if ''.join(arr[i:i+len(pat)]) == pat:
            print 'Found @ %d' % i    

print

findPattern(fname, pattern)

So from the new code replaced above, I'm getting what's below. I know it's something dumb like the array not being declared but I'm not exactly sure the python syntax for that, doesn't an array need to have a set size when you declare it?

lynx:desktop $ python hw3.py

Enter filename: declaration.txt
Enter pattern: become

Traceback (most recent call last):
  File "hw3.py", line 25, in <module>
    findPattern(fname, pattern)
  File "hw3.py", line 17, in findPattern
    arr = array.array('c', open(fname, 'rb').read())
NameError: global name 'array' is not defined

EDIT: And, completed! Thanks guys. This is how I finagled it..

#Iterate through
for i in xrange(len(arr)):

    #Check for endline to increment linePos
    if arr[i] == '\n':
        linePos = linePos + 1
        colPos = i

    #Compare a chunk of array the same size
    #as pat with pat itself
    if ''.join(arr[i:i+len(pat)]) == pat:

        #Account for newline with absolute position
        resultPos = i - colPos
        print 'Found @ %d on line %d' % (resultPos, linePos)

Related Questions