简体   繁体   中英

When printing out lines from a file, in the python executor it has a line gap between each line

I have got ASCII art in a file which I want to print out, however, when I go to print it out it leaves one line per line in the file, making the ASCII art not look like art.

I've got it working and it's fully functional, but I was wondering how I would get rid of the one line problem.

if user_input == "4":
    print('''You have selected [4] Display ASCII art.
        You will enter a file name, and the ASCII art will be printed..''')
    file_name = input("Enter file name: ")
    new_file_name = file_name + '.txt'
    with open(new_file_name) as file_handle:
        for line in file_handle:
            print(line)
            time.sleep(0.3)
    printProgramRestart()

I want the new output to look like art, all clustered into one but it doesn't.

print function has end parameter (as explained in the comments)

so when printing a line use:

print(line, end='')

Every time you print it will be on a new line, and when you read the .txt file it already includes newline signs (\\n). You can either print everything at the same time instead, or add an .strip('\\n') which removes the newlines:

        for line in file_handle:
            print(line.strip('\n'))
            time.sleep(0.3)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM