See code below where I try to use 2 different functions to read different parts from a text file (essentially a save file for a game board). The first one tries to read the first 5 lines and assign them to a matrix (list of lists). The second one tries to read the 6th line and assign it to a string. However I can't seem to get the code to work. Any ideas?
def load_board():
with open("savefile.txt","r") as savefile:
loadBoard = [line.strip().split(",") for line in savefile]
return loadBoard
def load_side():
with open("savefile.txt","r") as savefile:
loadBoard = [line.strip() for line in savefile]
loadSide = loadBoard.pop()
return loadSide
savefile.txt looks like this:
R,R,R,M,R
R,R,R,R,-
R,R,M,R,R
R,R,R,R,R
M,R,R,R,R
M
in both functions you are reading complete file. You need read all lines and then use required lines , see below example:
def load_board():
with open("savefile.txt","r") as savefile:
lines = savefile.readlines()
loadBoard = [line.strip().split(",") for line in lines[:5]]
loadside = lines[6]
return loadBoard, loadside
Instead of opening the file in each function, take an already open file as an argument:
import itertools
# Pre-condition: the input is at the beginning of the file
def load_board(savefile):
return [line.strip().split(",") for line in itertools.islice(savefile, 5)]
# Pre-condition: the first 5 lines have already been read
def load_side(savefile):
return next(savefile).strip()
Then open the file once before calling each function. Note that load_from_file
should be used to ensure that load_board
and load_side
are called in the correct order and with no other reads from savefile
to break the pre-conditions.
def load_from_file(fname):
with open(fname) as savefile:
board = load_board(savefile)
side = load_side(savefile)
return board, side
loadBoard, loadSide = load_from_file("savefile.txt")
savefile
as in code, is a file instance, not a list of lines. Instead, use savefile.readlines()
for all the lines. Also,
loadBoard = [line.strip().split(",") for line in savefile.readlines()]
would give you all 6 lines, not just 5. So loadBoard[-1]
would be [M]
.
Why not load the file once and then use the output to parse out what you want?
def load_save(save_filename):
with open(save_filename, 'r') as save_file:
return save_file.read().split('\n') # Return a list of all lines from the save file
def load_board(save_lines):
return [line.strip().split(',') for line in save_lines[:5]] # Won't include last line (side line)
def load_side(save_lines):
return [save_lines[5:].strip()] # Just the last line
This can be used like so:
save_lines = load_save('savefile.txt')
board = load_board(save_lines)
side = load_side(save_lines)
You can also enumerate over the file object.
def load_board():
with open("savefile.txt","r") as savefile:
loadBoard = [line.strip().split(",") for i, line in enumerate(savefile) if i<=4]
return loadBoard
def load_side():
with open("savefile.txt","r") as savefile:
loadBoard = [line.strip() for i, line in enumerate(savefile) if i==5] #only 6th ( index starts at 0 )
loadSide = loadBoard.pop()
return loadSide
print(load_board())
print(load_side())
Output
[['R', 'R', 'R', 'M', 'R'], ['R', 'R', 'R', 'R', '-'], ['R', 'R', 'M', 'R', 'R'], ['R', 'R', 'R', 'R', 'R'], ['M', 'R', 'R', 'R', 'R']]
M
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.