How to loop through previous lines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atrisa
    New Member
    • Sep 2010
    • 22

    How to loop through previous lines

    Hi everyone. I have a text file looking like this:

    Book ID: 1
    Availability: on shelf > 3 borrows > 2
    Name: Bread and Wine
    Writer: Silone

    Book ID: 2
    Availability: on shelf > 4 borrows > 1
    Name: Fontamara
    Writer: Silone

    Book ID: 3
    Availability: on shelf > 3 borrows > 2
    Name: Poirot
    Writer: Agatha Christie

    Book ID: 4
    Availability: on shelf > 2 borrows > 2
    Name: Miss Marple
    Writer: Agatha Christie

    and so on. I want to count the number of books by each writer and the total amount of their books been borrowed so far, to have a list like this:
    -------------
    Siolne 2 3
    A. Christie 2 4
    ---------------

    I have no idea how to go through this kind of blocks and specially grab a previous line from the block. Any help is appreciated.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Initialize a dictionary dd = {}
    Loop through each line of text
    If the line starts with "Writer", split the line on the colon character, add the writer to the dictionary as a key with a quantity of one.
    If the writer is already in the dictionary, increment the value by one
    OR the writer could be added this way:
    Code:
        v = dd.get('Agatha Christie', 0)
        dd['Agatha Christie'] = v+1

    Comment

    • Atrisa
      New Member
      • Sep 2010
      • 22

      #3
      This is what I did, but don't know why it doesn't print anything:

      Code:
      f = open('test.txt').read()
      
      dd = {}
      for line in f.split('\n\n'):
          if line.startswith('Writer'):
              writer = line.split(':')[0]
              dd.get(writer, 0)
              dd[writer] += 1
      print dd

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The way you did it, use dd.setdefault() instead of dd.get(). If you want the author's name, use index [1] instead of [0].

        Comment

        • Atrisa
          New Member
          • Sep 2010
          • 22

          #5
          Still the dictionary is empty.

          Comment

          • Atrisa
            New Member
            • Sep 2010
            • 22

            #6
            Now it is working.

            Code:
            dd = {}
            for line in open('test.txt'):
                if line.startswith('Writer'):
                    writer = line.split(':')[1].strip('\n')
                    dd.setdefault(writer, 0)
                    dd[writer] += 1
            print dd

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Good job Atrisa!

              BV

              Comment

              • Atrisa
                New Member
                • Sep 2010
                • 22

                #8
                Thanks BV for your hints, helps and instructions:)

                Comment

                Working...