Search code examples
pythonlistobjectargumentsfeed

Python - feed an object with a list


So I have the following code, which merges two text files (strings) into a list. This list should then feed an object.

cat file1.txt

['2.02', '-3.88', '15.25']
['4.40', '-2.36', '14.97']
['5.44', '0.34', '7.34']
['5.76', '0.41', '7.60']
['5.35', '0.19', '13.95']

cat file2.txt

['P1']
['P2']
['P3']
['P4']
['P5']

Code

from ast import literal_eval

#combine both files and return a list
def combiner(infile1, infile2):
    with open(infile1) as f1, open(infile2) as f2:
        f1_lists = (literal_eval(line) for line in f1)
        f2_lists = (literal_eval(line) for line in f2)
        for l1, l2 in zip(f1_lists, f2_lists):
            l3 = l1 + l2
            return l3


class Compass:

def __init__(self, coordx, coordy, coordz, positivity):
        self.coordX = coordx
        self.coordY = coordy
        self.coordZ = coordz
        self.posit = posit

def main():
    file1 = "file1.txt"
    file2 = "file2.txt"
    args = combiner(file1, file2)
    c = Compass(*args)
    print c.coordX + ' ' + c.coordY + ' ' + c.coordZ + ' ' + c.posit

if __name__ == '__main__':
    main()

output

2.02 - 3.88 15.25 P1

It outputs just the first list of course. How would you do to keep feeding until the list is over? Maybe using the lines from the file1.txt?


Solution

  • The first problem is that you are returning only the first line from a list from combiner(). Solution of this is to make a list l3, and then keep appending to it the new lines and return it after the loop has exhausted.

    The second problem is that you have 5 coordinates and positivities, therefore you also need 5 instances of Compass. Solution of this is to create a list of instances.

    Solving the above problems, your code will look like this:

    from ast import literal_eval
    
    #combine both files and return a list
    def combiner(infile1, infile2):
        with open(infile1) as f1, open(infile2) as f2:
            l3 = [] # this is a list for storing all the lines from both files
            f1_lists = (literal_eval(line) for line in f1)
            f2_lists = (literal_eval(line) for line in f2)
            for l1, l2 in zip(f1_lists, f2_lists):
                l3.append(l1 + l2)
            return l3 # Return all the lines from both text files
    
    
    class Compass:
    
        def __init__(self, coordx, coordy, coordz, positivity):
                self.coordX = coordx
                self.coordY = coordy
                self.coordZ = coordz
                self.posit = positivity
    
    def main():
        file1 = "file1.txt"
        file2 = "file2.txt"
        args = combiner(file1, file2)
        # You have 5 different Compass coordinates, therefore you need 5 different Compass instances
        # Therefore make a list of Compass instances
        compasslist = [ Compass(args[i][0], args[i][1], args[i][2], args[i][3]) for i in range(len(args))]
    
        # args[i][0] is coordx of i-th line (file1.txt), args[i][1] is coordy of i-th line (file1.txt),
        # args[i][2] is coordz of i-th line (file1.txt), and args[i][3] is positivity of i-th line (file2.txt)
    
        # Lets print all the Compass instances
        for i in range(len(args)):
            print compasslist[i].coordX + ' ' + compasslist[i].coordY + ' ' + compasslist[i].coordZ + ' ' + compasslist[i].posit
    
    if __name__ == '__main__':
        main()