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

3 Answers3

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 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 – thetaken477 May 16 '19 at 14:17
  • 2
    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). – chepner May 16 '19 at 14:24
  • how could I test that this code works, If I "print(data["room1North"]) it won't print "CP" – thetaken477 May 16 '19 at 14:39
  • @thetaken477 this works on my machine, does the snippet give you and IndexError? – Chi May 16 '19 at 16:12
  • 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)` – thetaken477 May 16 '19 at 16:39
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')]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Amesh Jayaweera
  • 216
  • 2
  • 11
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"]

zerocool
  • 308
  • 1
  • 10
  • 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") – thetaken477 May 16 '19 at 14:25
  • `if rooms["room1North"] == CP: print("you can't go there")` – zerocool May 16 '19 at 14:27
  • I get an error if I try testing this code: rooms[currentRoom[0]] = currentRoom[1] IndexError: list index out of range – thetaken477 May 16 '19 at 14:31
  • I still get the same error list index out of range on the same line of code – thetaken477 May 16 '19 at 14:42
  • 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 – zerocool May 16 '19 at 14:43
  • Im still getting the error I defined the dictionary room via "= {}", do you have the same txt file as me – thetaken477 May 16 '19 at 15:25
  • the dictionary is called rooms in my code. not room. I copied the text from your question into a text file. – zerocool May 16 '19 at 15:26