views:

128

answers:

1

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)
+1  A: 

The only way to get text data into an array is as chars:

a = array.array('c', open(filename, 'rb').read())

From there, you can simply iterate over it and convert each subarray with the same length as your substring to a string to compare:

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

This is however deeply un-pythonic and painfully slow.

If by array you mean a list (the two terms have very different meanings in Python):

pos = 0
for line in open(filename):
    for i in xrange(len(line)):
        if line[i:i+len(substring)] == substring:
           print 'Found @ %d!' % (pos + i)
    pos += len(line) + 2 # 1 if on Linux

This is also slow and un-pythonic, but vaguely less so than the previous option. If any of these really is what you've been asked to do, your teacher probably shouldn't be teaching Python. :p

Vlad
Do I need to declare array somehow?
John Redyns
You need to import it, actually. It's a module. :)
Vlad
So similar to #import <std.io> in c?Just 'import array' it seems?
John Redyns
Dumb question, perfect. I love that we didn't have to deal with newline characters, because if I enter the last word on the first line with the first word on the second line, it isn't supposed to find it, and it doesn't because of that newline \n char. In addition, I can now use that as a count to see what line I'm on, right? Let's see how I can work that in.The only problem I can see is that, the character count that we have keeps going, not worrying about what line. I believe I need to start that back at 0 for each line.
John Redyns
Vlad
What would be a/the pythonic way to accomplish this?
DaClown
First instance: print open(filename, 'rb').read().find(substring) All instances: data = open(filename, 'rb').read(); i = 0; while 1: i = data.find(substring, i); if i >= 0: print i; i += 1; else: break (replace ; with CR/LF and try to figure out indentation)
Vlad