0

Inside the text file I currently have:

    tickets = []
    ticketPrice = 2.20
    ticketsNo =  150
    income = ticketPrice*ticketsNo
    ticketHi = 54
    limit = 1
    winners = []
    numbers = []
    winningTickets = []

How would I now read the file and have it create variables and store values accordingly? I know about

    with open("file.txt", "r") as f:
        //do stuff

but I don't know how to implement it in the fashion I'm after. Thanks in advance

11
  • Hullo there. You could probably go about using a dictionary. You could start off with f.read().splitlines() to divide the file into a list of lines. Then split each individual line at the equal sign with split(' = '). You can then store the left-hand-side as the dictionary's key and the right-hand-side as the value. You can parse the constants using eval(). With income = ticketPrice*ticketsNo, I'm not so sure... Commented Nov 2, 2018 at 17:03
  • 1
    why don't you just import your file? It's valid Python source code... Commented Nov 2, 2018 at 20:57
  • @juanpa.arrivillaga How would I do this? I've never heard of importing files before. Commented Nov 2, 2018 at 21:48
  • 1
    import myfile Commented Nov 2, 2018 at 21:48
  • 1
    You are going to need to provide more details in the question itself. Commented Nov 2, 2018 at 21:53

2 Answers 2

1

If you just want to store some varibales out of your main code, I would create a python file with the exact same text you have there, call it config.py for example, and them simply import it like:

from config import *

Even if the file is in a random path you could do

sys.path.append("config_path")
from config import *

If you just have to execute a random txt and that is the only option, you could use exec, which is very unrecommended, so you should think how did you get to that point. But here it is:

with open('file.txt', 'r') as file:
    code = file.read()
exec(code, globals())
Sign up to request clarification or add additional context in comments.

Comments

0

Using assignment unpacking:

with open("file.txt", "r") as f:
    data = f.readlines()


tickets, ticketPrice, ticketsNo, income, ticketHi, limit, winners, 
numbers, winningTickets = [d.split('=')[1].split('\n')[0] for d in data]

The result of this unpacking will be of type string. For int values, you will have to parse the variable eg. int(limit).

4 Comments

I'm not sure if I've formatted this correctly, however I'm getting an error on line 51: number = r.randint(1, limit) saying I should be providing a string not an integer? Please see my code for more details (code goes onto more than one document)
I've tried that code same issue. What I don't get though is why it's saying it should be a string, because on the docs it says to put an integer.
The variable limit that you are trying to put in the randint() function is a string, it should be an integer. You have to convert the variable from string to an integer.
That's what I thought, but the error definitely says that it expected a string and got an integer... The error I get is TypeError: must be str, not int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.