Blank Line File Reading

36 Views Asked by At

I am attempting to make an apple ][ (2) emulator for fun.

What I have tried is things like:

if line is '' then skip line

I am currently building the lines part. A file may read something like this:

1 [line of code 1]
2
3
4
5 [line of code 2]
6
7
8
9
10 [line of code 3]

(For those of you who don't know, with apple 2, you put ~10 lines of white space between lines, because there is no copy and paste)

If somebody used the 'list' command, it should print out:

1 [line of code 1]
5 [line of code 2]
10 [line of code 3]

Everything I try usually ends up doing one of the 2 following if I try to remove blank lines

2
4
6
8
10

OR

2 [
4 
6
8
10 [li
1

There are 1 best solutions below

0
On

You simply use:

for idx, ele in enumerate(lines):
    lines[idx] = ele.replace('\n', '')

This removes the '\n' from the file