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.
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. Eg:
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())
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.