Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:09:30+00:00 2026-05-17T17:09:30+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:09:31+00:00Added an answer on May 17, 2026 at 5:09 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I guess this question has been asked a lot of times here. I
Alright, I don't know if anyone has tried to do this yet, however. I
Alright I want to create a .csv file in C#. I have been looking
Alright I'm a bit baffled by this one. Changing an unrelated int property on
Alright, Im going about this, with what is probably a really complicated solution, but
Alright, I'm trying to understand follow sets and I think I got it except
Alright, I'm just trying to learn about using Contact information, but I'm a bit
Alright, I have a div which is 50px (height), and the width does not
Alright I'm going to create a fairly complex form to post via AJAX a
Alright, I'm trying to pass a Dictionary, where key = int, and value =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.