I need to write a function that read from a file and put every some lines as one cell in a list.
I need to copy the lines between 1 to 2 and from 2 to the end and put each of them in different cell in the list, without the lines that contains the number and empty lines.
I dont have an idea how make it work.
the file:
; 1
####
# .#
# ###
#*@ #
# $ #
# ###
####
; 2
######
# #
# #@ #
# $* #
# .* #
# #
######
my code:
def loader(filename):
levels=[[]]
f=open(filename, "r")
x=[]
for line in f:
if ";" not in line:
x.append(line)
else:
levels.append(x)
f.close()
return levels
output:
cell 1:
####
# .#
# ###
#*@ #
# $ #
# ###
####
cell 2:
######
# #
# #@ #
# $* #
# .* #
# #
######
Final best solution:
If you use
re, you can find anything fromto
Here’s the code:
older: (compensates for numbers, and gets rid of blank lines completely)
OR
To print the output:
Or individually: