#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Unhappy Python Programming Question Help


    Hello!

    I was working on this Python question. I have laid out all the def functions and I'm working on the first 2 definitions: printsection1(animals, station1, station2) and printsection2(animals, station1, station2).
    For printsection1, I am having trouble building a fixed width column for my results.

    The file I used contained the following:

    #Comments

    a01:01-24-2011:s1
    a03:01-24-2011:s2


    a02:01-24-2011:s2
    a03:02-02-2011:s2
    a03:03-02-2011:s1
    a02:04-19-2011:s2

    #comments
    a01:05-14-2011:s2
    a02:06-11-2011:s2
    a03:07-12-2011:s1
    a01:08-19-2011:s1
    a03:09-19-2011:s1
    a03:10-19-2011:s2
    a03:11-19-2011:s1
    a03:12-19-2011:s2


    And I want my output for printsection1 to look like this:

    Enter name of input file >animallog.txt
    Number of times each animal visited each station :
    Animal Id Station 1 Station 2
    a01 2 1
    a02 0 3
    a03 4 4
    ============================================================


    So far in my program this is what I've got:

    Code:
    import os.path
    try:
        def main():
            filename1=input('Enter name of input file >')
            infile=open(filename1, 'r')
            
            station1 ={} #a dictionary that records frequency of visit of each animal to station 1
            station2 ={} #a dictionary that records frequency of visit of each animal to station 2
            record=() #a tuple representing a line/record in the file
            items=list(record) #-> a list of records in the file
            animals = []# a list of all animals
            i=0
            animaldict={}
            for lines in infile:
                lines=lines.strip("\n")
                if((lines!="") and (lines[0] != "#")):
                    i=i+1
                    animal, date, station = lines.split(":")     
                    record = (animal, station)
                    if record not in animaldict:
                        animaldict[record] = 1
                    else:
                        animaldict[record] += 1
                    
                    animals = []
                    for key, value in animaldict.items():
                        if key[0] not in animals:
                            animals.append(key[0])
                            
                        if (key[1] == 's1'):
                            station1[key[0]] = value
                        if (key[1] == 's2'):
                            station2[key[0]] = value
            print('animals:', animals)
            print('station 1:', station1)
            print('station 2:', station2)
            print(printsection1(animals, station1, station2))
            print(printsection2(animals, station1, station2))
            print(section3(animals,station1,station2))
            print(printsection4(items))
            
            infile.close()
        
        
        def printsection1(animals, station1, station2):
        #Number of times each animal visited each station
            print("Number of times each animal visited each station :")
            print("Animal Id             Station 1             Station 2")        
            #EVERYTHING IS OKAY UP TO HERE
            for a in sorted(animals):
                print('%3s'%a,end=' '*19)
                vals1 = 0
                vals2=0
                for s in sorted(station1):
                    if s in station1:
                        vals1 = station1[s]
                    print('%1d'%vals1,end=' '*20)
                for s in sorted(station2):
                    if s in station2:
                        vals2 = station2[s]
                    print('%4d'%vals1,end=' '*20)
                    
                print()
            print("="*60)
            
            return
        
        def printsection2(animals, station1, station2):
            #Animals that visited both stations at least 4 times
            print("Animals that visited both stations at least 4 times: ")
            
            for a in sorted(animals):
                x = 'false'
                for key, value in sorted(station1.items()):
                    if  station1[key] >= 4:
                        x = 'true'
                        
                for key, value in sorted(station2.items()):
                    if  station2[key] >= 4:
                        x = 'true'
                        
                if x is 'true':
                    print('%3s'%a, end=' ')
                    print("")
                    
            print("="*60)                
            return
        def section3(animals,station1,station2):
                
            return
        def printsection4(items):
            #print the month that has the most number of visits to the stations.
            
            
            return
        main()
        
    except IOError:
        print('File does not exist')


    I would love it if someone can give me advice on printsection1, I would greatly appreciate it!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    When you create a post with code in it, please press the # hashtag symbol and just paste your code in the dialogbox that come up, so we can see your indent.

    To get a fixed width with your print output, see this example:

    Code:
    print("very long text".ljust(15), "test")
    print("shorttext".ljust(15), "test")
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Smile


    Originally Posted by leonnaley2
    When you create a post with code in it, please press the # hashtag symbol and just paste your code in the dialogbox that come up, so we can see your indent.

    To get a fixed width with your print output, see this example:

    Code:
    print("very long text".ljust(15), "test")
    print("shorttext".ljust(15), "test")
    Oh sorry about that! I just fixed it so maybe that will help you interpret it better.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    is this what you are looking for?
    Code:
    def printsection1(animals, station1, station2):
        #Number of times each animal visited each station
            iSpacing = 15
            print("Number of times each animal visited each station :")
            print("Animal Id".ljust(iSpacing), "Station 1".ljust(iSpacing), "Station 2".ljust(iSpacing), sep="")
            #EVERYTHING IS OKAY UP TO HERE
            for a in sorted(animals):
                print(a.ljust(iSpacing), end="")
                vals1 = 0
                vals2 = 0
                for s in sorted(station1):
                    if s in station1:
                        vals1 = station1[s]
                    print(str(vals1).ljust(iSpacing), end="")
                for s in sorted(station2):
                    if s in station2:
                        vals2 = station2[s]
                    print(str(vals1).ljust(iSpacing), end="")
    
                print()
            print("="*60)
    
            return
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by leonnaley2
    is this what you are looking for?
    Code:
    def printsection1(animals, station1, station2):
        #Number of times each animal visited each station
            iSpacing = 15
            print("Number of times each animal visited each station :")
            print("Animal Id".ljust(iSpacing), "Station 1".ljust(iSpacing), "Station 2".ljust(iSpacing), sep="")
            #EVERYTHING IS OKAY UP TO HERE
            for a in sorted(animals):
                print(a.ljust(iSpacing), end="")
                vals1 = 0
                vals2 = 0
                for s in sorted(station1):
                    if s in station1:
                        vals1 = station1[s]
                    print(str(vals1).ljust(iSpacing), end="")
                for s in sorted(station2):
                    if s in station2:
                        vals2 = station2[s]
                    print(str(vals1).ljust(iSpacing), end="")
    
                print()
            print("="*60)
    
            return
    Yes that is perfect! I do get a fixed width column table from this, however my values are not printing out properly. I know I am able to get the right values from the main function, however I can't seem to display it properly in the table, even though the table itself is fine. This is my result for the table in printsection1:

    Code:
    Number of times each animal visited each station :
    Animal Id      Station 1      Station 2      
    a01            2              4              4              4              4              
    a02            2              4              4              4              4              
    a03            2              4              4              4              4              
    ============================================================
    None
    I should actually be getting something like this:

    Code:
    Number of times each animal visited each station :
    Animal Id      Station 1      Station 2
    a01            2              1
    a02            0              3
    a03            4              4
    ============================================================

IMN logo majestic logo threadwatch logo seochat tools logo