Q:

Python program to store values in an external file

How would I go about creating a python program to store a range of values (ie. apples=3, bananas=5, carrots=12.5) in an external file where I can later use those values for calculations later on?
Currently, I am able to print to and read from text files using fileinput and fileoutput, but I am unable to use those values in calculations later on.
with open("values.txt","w") as fileinput:
    fileinput.write(value)

An example of what I am looking for is first being able to type a variable name (eg. Apples), then type a number or other value (eg. 3.3) and then print those values to the values.txt. That way, a separate program could view values.txt to be able to use the value of apples=3.3 in a calculation (eg. apples*3=9.9)

A:

Assuming that you want to store fruit and values pairs into a dictionary (but you can adapt it to any data structure):
Writing to file:
fruits = {"apple":12, "banana":4}
with open("test.txt", 'w') as out:
    for fruit,number in fruits.iteritems():
        line = fruit + '=' + str(number) + '\n'
        out.write(line)

Parse from file into dictionary
emptyDict = {}
with open("test.txt", 'r') as infile:
    for line in infile:
        tokens = line.strip().split('=')
        fruit = tokens[0]
        number = tokens[1]
        emptyDict[fruit] = float(number)

