I need to write a function that read a file and put in list every level of a game in 1 cell.
every cell contains the rows of the level as strings, and every row is an element of the cell.
The final cell should be without the numbers or the words that appears before the level and without “\n” or empty cells.
I implemented this function, but it not working so well for levels looking like this.
some have any idea?
level from the file
; 1
####
# .#
# ###
#*@ #
# $ #
# ###
####
as cell in the list:
['####', '# .#', '# ###', '#*@ #', '# $ #', '# ###', '####']
example of levels I have problem with:
; 154 'Take the long way home.'
############################
# #
# ######################## #
# # # #
# # #################### # #
# # # # # #
# # # ################ # # #
# # # # # # # #
# # # # ############ # # # #
# # # # # # # # #
# # # # # ############ # # #
# # # # # # # #
# # # # ################ # #
# # # # # #
##$# # #################### #
#. @ # #
#############################
looking somthing like this:
["54 'Take the long way home.'", '', ' ############################', ' # #', ' # ######################## #', ' # # # #', ' # # #################### # #', ' # # # # # #', ' # # # ################ # # #', ' # # # # # # # #', ' # # # # ############ # # # #', ' # # # # # # # # #', ' # # # # # ############ # # #', ' # # # # # # # #', ' # # # # ################ # #', ' # # # # # #', '##$# # #################### #', '#. @ # #', '#############################']
my code:
def loader(filename):
tmp=[]
levels=[]
f=open(filename, "r")
f=f.read().split(';')
for line in f:
tmp.append(line[2:].strip())
tmp.remove("")
for i in tmp:
i=i.split("\n")
levels.append(i)
print levels
It’s kinda hard to follow exactly what you’re trying to accomplish, but I think this function will give you what you want.
Edit:
Oh, I didn’t realize that there were multiple levels per file.
How about this then: