簡體   English   中英

從文件中打印出行時,在python執行程序中,每行之間都有一個行間隙

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

我在要打印的文件中有ASCII藝術,但是,當我打印出來時,它在文件中每行留下一行,使得ASCII藝術看起來不像藝術。

我已經完成它並且它功能齊全,但我想知道如何擺脫一線問題。

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()

我希望新的輸出看起來像藝術品,所有產品都聚集成一個,但事實並非如此。

print函數有end參數(如注釋中所述)

所以當打印一行時使用:

print(line, end='')

每次打印時都會在新行上,當您讀取.txt文件時,它已經包含換行符號(\\ n)。 您可以同時打印所有內容,也可以添加刪除換行符的.strip('\\ n'):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM