Why is print() not working anymore with python?

后端 未结 3 724
清歌不尽
清歌不尽 2021-01-28 03:29

in fact this should not be a problem because it\'s fairly basic. I just want to print out an array of categories, but in one line and separated by comma.

for ent         


        
3条回答
  •  萌比男神i
    2021-01-28 03:42

    You are almost certainly experiencing output buffering by line. The output buffer is flushed every time you complete a line, but by suppressing the newline you never fill the buffer far enough to force a flush.

    You can force a flush using flush=True (Python 3.3 and up) or by calling the flush() method on sys.stdout:

    for entry in categories:
        print(entry, ", ", end='', flush=True)
    

    You could simplify that a little, make , the end value:

    for entry in categories:
        print(entry, end=', ', flush=True)
    

    to eliminate the space between the entry and the comma.

    Alternatively, print the categories as one string by using the comma as the sep separator argument:

    print(*categories, sep=', ')
    

提交回复
热议问题