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

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Bob
  • 11
  • 1
  • 2
  • 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... – TrebledJ Nov 02 '18 at 17:03
  • 1
    why don't you just import your file? It's valid Python source code... – juanpa.arrivillaga Nov 02 '18 at 20:57
  • @juanpa.arrivillaga How would I do this? I've never heard of importing files before. – Bob Nov 02 '18 at 21:48
  • 1
  • @juanpa.arrivillaga That gives the following error: ```Traceback (most recent call last): File "main.py", line 3, in import vars.py ModuleNotFoundError: No module named 'vars.py'``` – Bob Nov 02 '18 at 21:51
  • Use `import vars` – juanpa.arrivillaga Nov 02 '18 at 21:52
  • @juanpa.arrivillaga gives same issue? The files are in same directory – Bob Nov 02 '18 at 21:53
  • 1
    You are going to need to provide more details in the question itself. – juanpa.arrivillaga Nov 02 '18 at 21:53
  • @juanpa.arrivillaga nvm, it was just because I was on an online editor. After transferring files to my Desktop it works. How would I know use those variables? I've tried both ticketPrice and vars.ticketPrice – Bob Nov 02 '18 at 22:02
  • if you `import vars` then it should be `vars.ticketPrice` – juanpa.arrivillaga Nov 02 '18 at 22:03
  • `AttributeError: module 'vars' has no attribute 'ticketPrice'`. `tickets = [] ticketPrice = 2.20 ticketsNo = 150 income = ticketPrice*ticketsNo ticketHi = 54 limit = 1 winners = [] numbers = [] winningTickets = []` is my current code (each variable is on a different line, having issues with formatting) – Bob Nov 02 '18 at 22:06

2 Answers2

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())
thisisrandy
  • 2,660
  • 2
  • 12
  • 25
Alexgar
  • 21
  • 3
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).

Franndy Abreu
  • 186
  • 2
  • 12
  • 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](https://repl.it/repls/CooperativeGratefulSandboxes) for more details (code goes onto more than one document) – Bob Nov 02 '18 at 18:50
  • 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](https://docs.python.org/2/library/random.html#random.randint) it says to put an integer. – Bob Nov 02 '18 at 21:47
  • 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. – Franndy Abreu Nov 02 '18 at 21:52
  • 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` – Bob Nov 02 '18 at 22:03