3 Replies - 692 Views - Last Post: 27 October 2014 - 08:48 PM Rate Topic: -----

#1 Deadeye007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 14

Program to add line numbers based on line contents

Posted 22 October 2014 - 08:00 AM

Hello, I'll preface this by saying I'm new to programming and I am currently taking a course where I there are assignments given in Python but we are not taught Python, so I've been attempting to learn, but it's been difficult without any previous programming knowledge. I apologize for what is most likely a very simple question.

I am trying to create a program that will read a file and based on whether the lines in that file contain a certain word, it is assigned a line number (First line would be 0, the next numbered line would be 1, etc.) or if the line does not contain one of these words it is not given a number.

An example would be:

File inputted

public x
ld 2
st 4
x: y:
dw 5

What would come out to standard output

public x
0 ld 2
1 st 4
x: y:
2 dw 5

Because "ld", "st", and "dw" are contained in the lines they are numbered, the others are not.

I am stuck on how to accomplish this adding of the line number. What I've tried to do is open the file, have each line added to a list. Then go through the list and if the line contains one of the key words I would want to add a number. As far as I know though, I cannot add the number to the line while it is in the list. So I thought maybe I could break each line up into it's own list (So each word would be a list entry) and then add the number in the beginning of the list and then make the list back into a single line later for printing. That would require me to make a list for every single line that had to be numbered though, and I know that's not the proper way of going about it and also the number of lists required would always change based on the inputted file.


Hopefully I explained well enough what I'm trying to accomplish. I am NOT looking for any code or any step by step instructions. I am asking for some sort of general direction of what I should look into and figure out to accomplish this because I only have a very limited knowledge base of the capabilities of python which makes searching on the internet not very helpful, since I am not sure what to actually search for.

Is This A Good Question/Topic? 0
  • +

Replies To: Program to add line numbers based on line contents

#2 jon.kiparsky  Icon User is online

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Program to add line numbers based on line contents

Posted 22 October 2014 - 08:26 AM

So let's try a simpler version: let's say you've got the lines of the file as a list, and you want to output each line. That's not that hard, I suppose, but go ahead and do it. Then, make it a little harder: output the line just if it happens to contain one of the key words on your list. Still not hard? Okay, then change your program a little so it prints all the lines again, but this time, put some mark in front of the ones that have the words on the list. (let's say you put a '***' in front of those lines), and no mark in front of the ones that don't have a word from the list. Still with me? If you haven't written each of those programs, stop and do that. I can wait.


...



...


Okay, now for the capper. One last change. First, define a variable called "count" and set it to zero at the start of your program. Then, if you come across a line that has one of your keywords, print the current value of counter followed by the line, then increment counter. Otherwise, just print the line.
And then you're done.


If you want a good resource on python, I've been hearing good things about "Python the Hard Way". Free online book, you might want to take a look.
Was This Post Helpful? 0
  • +
  • -

#3 Deadeye007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 14

Re: Program to add line numbers based on line contents

Posted 22 October 2014 - 09:45 AM

Ok, thank you very much for taking the time to respond. Coming at it in more incremental steps is a good idea, will work on it and report back. Also thanks for the book recommendation, always looking for new resources.
Was This Post Helpful? 0
  • +
  • -

#4 Deadeye007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 14

Re: Program to add line numbers based on line contents

Posted 27 October 2014 - 08:48 PM

Just wanted to come back and say I figured it out. I did come across one problem in that originally I did not consider if one of the keywords were in a comment (For instance a comment could be ";this line will add x and y") that a line number should not be added. I worked out that I could just get the index of the character that denotes a comment ';' and compare it to the index of the keyword. I included a portion of my code. There are many keywords that need to be checked for so I only included one since the other are identical besides the 'add' being 'sub' for example. I feel like maybe the first else: statement might be unnecessary, going to look at it again tomorrow, seems unnecessarily messy. Anyway, just wanted to say thanks again for the assistance jon.kiparsky!

fileList = [lines.rstrip() for lines in open ('test.txt')] #Creates list of lines without \n
numList = []
count = -1
for line in fileList:    #Goes through each element of list
    if 'add' in line: 
        if ';' in line:
            if line.index(';') > line.index('add'):     #If ';' appears after 'add' then line num is added
                count +=1
                numList.append(str(count)+ ' ' + '>' + '  ' +line)                
            else:    # ';' appears before 'add' no line num added
                numList.append('  ' + '>' + '  '+line) 
        else:    # 'add' is in line and no ';' then add line num
            count +=1 
            numList.append(str(count) + ' ' + '>' + '  ' +line)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1