开发者

Open file into array, search for string and return value

开发者 https://www.devze.com 2023-01-19 12:22 出处:网络
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.

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 开发者_如何学Pythontoo 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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消