teepa
teepa

Reputation: 95

Read from file and call function

I have a task in which I need to read from text file and call a function. The text file is following:

black,20,10,3,1
red,10,20,4,3
blue,10,-20,-4,3

My defined function takes five parameters, which are split in the text flile by commas. This is what I have so far:

with open(textfile) as source:
    for i in source.readlines():
        a = split(",")

But here I have no idea how to call the function with the read line from source.

Any ideas?

Upvotes: 0

Views: 639

Answers (2)

teepa
teepa

Reputation: 95

I ended up with this and it is working.

def piirra_tiedostosta(tiedosto):
    with open(tiedosto) as source:
        for i in source.read().splitlines():
        c, a, r, n, w = i.split(",")
        a = int(a)
        r = int(r)
        n = float(n)
        w = int(w.strip())

Now I have all the variables separated from eachother, and I can call the function.

f(c, a, r, n, w)

Upvotes: 0

fuglede
fuglede

Reputation: 18201

If your function is f, you could simply call f(*a).

Upvotes: 1

Related Questions