0

I'm trying to print a text file that contains ASCII art in python.

I realize the easy way would be do something like this

with open(image, 'r') as f:
for line in f:
    print(line.rstrip())

but I want to print it using the curses library so that I can display other text along with the image. Here is what I've come up with so far.

lines=[]
with open('image.txt',"r",encoding="utf8") as f:
        lines.append(f.readlines())

for a in lines:
    char = str(("".join(a)))
    stdscr.addstr(y, x, char)

This code does 90% of the job but I cant get the image to shift to the right. I can choose which row the image begins on by changing the y in stdscr.addstr(y, x, char) but changing x has no effect on which column it starts in.

Is there a way to fix this? Thanks.

1 Answer 1

1

When you call lines.append(), you're taking the entire list returned by f.readlines(), and adding it to lines as a single item. for a in lines, then, loops only once, joining the elements of a (the entire file) back together and passing that to addstr(), which interprets the embedded line feeds, resetting each line after the first to the first column.

Instead of lines.append(), you either want lines.extend(), or, more likely, just lines = f.readlines(). You can then dispense with the join, although you should probably strip the line feeds. E.g.:

with open('image.txt',"r",encoding="utf8") as f:
    lines = f.readlines()

for a in lines:
    # set x and y here
    stdscr.addstr(y, x, a.rstrip())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.