4 Replies - 486 Views - Last Post: 10 March 2019 - 07:18 AM Rate Topic: -----

#1 valen80000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 19

Counting Specfic Lines

Posted 07 March 2019 - 05:39 PM

Hello Everyone,

I am new to the forum so the first thing I would like to start off by saying hello to everyone.

Now I am no Python expert but I know to count all the lines in a text file your Python code would look like:

count_lines = 0

with open('file.txt', 'a') as k:
     for line in k:
         count_lines += 1
print(count_lines)





Now, lets say you have a text file organised something like this:

Fruits:
1. Apples
2. Grapes
3. Pears
4. Bananas

Drinks:

1. Water
2. Soft Drink
3. Juice
4. Coffee

Transport:

1. Car
2. Truck
3. Van
4. Jeep

Instruments:

1. Guitar
2. Violin
3. Flute
4. Drum


Let's say I want to to only count the lines for Drinks & Transport, how would you about constructing such a script?

Would it be something like:

wordone = "Drinks"
wordtwo = "Transport"
count_lines = 0


with open('file.txt', 'a') as k:
     for line in k:
         if wordone = "Drinks" and wordtwo = "Transport"
         count_lines += 1
print(count_lines)




Anyone can help?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Counting Specfic Lines

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14924
  • View blog
  • Posts: 59,580
  • Joined: 12-June 08

Re: Counting Specfic Lines

Posted 07 March 2019 - 09:33 PM

What happens when you try it?
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 513
  • View blog
  • Posts: 1,632
  • Joined: 27-December 13

Re: Counting Specfic Lines

Posted 08 March 2019 - 05:49 AM

Hello valen80000, welcome to Dream.In.Code - I hope you will find it helpful!

In general, we try to help you help yourself.
That is, we don't hand out code (except for few lines) and we often ask 'silly' questions designed to spark your own thinking.

Like modi123_1's reply in post #2.
Both he and I know that your code will throw an error - but you will learn much more from getting this error and resolve the situation yourself.
When you are really stuck with a specific code chunk that you cannot remedy, you are more than welcome to come back here; please remember to include the error message, it may hold important information.

Please also take time to read and (hopefully) understand the error message before asking - the python error messages are often quite specific and understandable.

Welcome.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7419
  • View blog
  • Posts: 15,371
  • Joined: 16-October 07

Re: Counting Specfic Lines

Posted 08 March 2019 - 04:12 PM

First, with open('file.txt', 'r') as k: should NOT work. I'll admit, you caught me. Try 'r'.

To solve your problem, begin by printing:
count_lines = 0
# wtf, 'a' ?!? and k is a woking name
# with open('file.txt', 'a') as k: 
with open('file.txt', 'r') as fh:
    for line in fh:
        count_lines += 1
        # let's have a look at what we're actually counting
        print(count_lines, line)
print(count_lines)


Now that you can see your lines, try to apply your logic.

Perhaps, if you figured out some way to track the current section you're in. Since a section ends with ":", you should be able to identify it. Perhaps:
current_section = None
with open('file.txt', 'r') as fh:
    for line in fh:
        # your code here
        print(line, "#", current_section)



Possible results:
 # None
section change Fruits:
Fruits: # Fruits:
1. Apples # Fruits:
2. Grapes # Fruits:
3. Pears # Fruits:
4. Bananas # Fruits:
 # Fruits:
section change Drinks:
Drinks: # Drinks:
 # Drinks:
1. Water # Drinks:
2. Soft Drink # Drinks:
3. Juice # Drinks:
4. Coffee # Drinks:
 # Drinks:
section change Transport:
Transport: # Transport:
 # Transport:
1. Car # Transport:
2. Truck # Transport:
3. Van # Transport:
4. Jeep # Transport:
 # Transport:
section change Instruments:
Instruments: # Instruments:
 # Instruments:
1. Guitar # Instruments:
2. Violin # Instruments:
3. Flute # Instruments:
4. Drum # Instruments:



Once you have that logic worked out, you can worry out how to filter out lines that aren't actual items.
current_section = None
running_section_count = 0
with open('file.txt', 'r') as fh:
    for line in fh:
    ...



Possible results:
1 1. Apples
2 2. Grapes
3 3. Pears
4 4. Bananas
Fruits total count 4
1 1. Water
2 2. Soft Drink
3 3. Juice
4 4. Coffee
Drinks total count 4
1 1. Car
2 2. Truck
3 3. Van
4 4. Jeep
Transport total count 4
1 1. Guitar
2 2. Violin
3 3. Flute
4 4. Drum
Instruments total count 4



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 valen80000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-March 19

Re: Counting Specfic Lines

Posted 10 March 2019 - 07:18 AM

Thanks alot baavgai for the assist.

This will surely help.

Will try it and let you know how it goes.

Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1