0

This is linked to this question here I used the first answer, I tried changing the code but it didn't seem to work as that example has "[]" in the variables

I have a text file here:

room1North = CP
room1East = CP
room1South = OP
room1West = OP
room2North = OP
room2East = CP
room2South = EP
room2West = OP

I would like Python to create variables with the values in the text file so the variable "room1North = CP" in Python

I have the following code so far


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


room1North, room1East, room1South, room1West, room2North, room2Eeast, room2South, room2West  = [d.split('=')[1].split('\n')[0] for d in data]

I get the following error:

IndexError: list index out of range
5
  • You overwrite data each time without saving its value, thereby discarding the previous lines. Commented May 16, 2019 at 14:07
  • 2
    Possible duplicate of How do I create a variable number of variables? Commented May 16, 2019 at 14:07
  • This is not a duplicate because in that example it seems to be doing it from a dictionary inside python rather then have it from a text file Commented May 16, 2019 at 14:12
  • 1
    @thetaken477 re the duplicate, the source data type is not specified in that question Commented May 16, 2019 at 14:30
  • For debugging your code, try looking at data Commented May 16, 2019 at 17:21

3 Answers 3

3

You don't actually want separate variables; you want a single dict whose keys are read from the file.

with open("maze files.txt", "r") as f:
    data = {k:v for k, v in [line.strip().replace(' ', '').split("=") for line in f]}

# data["room1North"] == "CP"
# data["room1East"] == "CP"
# data["room1South"] == "OP"
# etc
Sign up to request clarification or add additional context in comments.

5 Comments

Can you explain further, does this mean I'm declaring in the Python file what the variable is, I want it to use the variable from the text file
You just use data["room1North"] instead of room1North in your subsequent code. In general, you don't want your variable names to depend on data from a file (or require the programmer to have advance knowledge of the precise contents of the file).
how could I test that this code works, If I "print(data["room1North"]) it won't print "CP"
@thetaken477 this works on my machine, does the snippet give you and IndexError?
no it gives a ValueError: data = {k:v for k, v in [line.strip().replace(' ', '').split("=") for line in f]} ValueError: not enough values to unpack (expected 2, got 1)
1

Change your code as bellow

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

room1North, room1East, room1South, room1West, room2North, room2Eeast, room2South, room2West  = [d.split('=')[1].split('\n')[0] for d in ''.join(data).split('\n')]

Comments

0

I think you'd have more luck using a dictionary rather than relying on pure variables.

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

rooms = {}
for i in data:
    currentRoom = i.replace(' ', '').strip().split('=')
    rooms[currentRoom[0]] = currentRoom[1]

What you'll be left with is a dictionary like the following

print(rooms)
#{'room1North ': ' CP', 'room1East ': ' CP', 'room1South ': ' OP', 'room1West ': ' OP', 'room2North ': ' OP', 'room2East ': ' CP', 'room2South ': ' EP', 'room2 West ': ' OP'}

You can reference each room and it's value by rooms["room1North"]

7 Comments

That would have been fine but what if I'm using the variable later on, for example if room1North = CP print("you can't go there")
if rooms["room1North"] == CP: print("you can't go there")
I get an error if I try testing this code: rooms[currentRoom[0]] = currentRoom[1] IndexError: list index out of range
I still get the same error list index out of range on the same line of code
sounds to me like you instantiated rooms as a list instead of a dictionary, but without seeing the code you're running I don't know. I'm using python3 and I've copy pasted that code. It runs without error on python 3.6.7
|

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.