Python中的PRint默認(rèn)是換行的
想要不換行輸出有兩種辦法:
1.print后加上","
############################
>>>print "Hello World",
############################
2.使用sys.stdout.write命令
############################
>>>sys.stdout.write("Hello World")
############################
但是,以上的命令在具體執(zhí)行時(shí),并不會(huì)實(shí)時(shí)顯示,每次都是在換行時(shí)才把整行指令打出來.
如果要實(shí)時(shí)顯示,需要在每次sys.stdout.write后面加上一行sys.stdout.flush()讓屏幕輸出
############################
sys.stdout.write("Hello World")
sys.stdout.flush()
sys.stdout.write("one line!")
sys.stdout.flush()
############################
附一段我用來控制write換行情況的代碼
############################
class ChangeLine: NewLine = True @classmethod def write_out(self,str,typename = 0):# 0 is "/n...../n" if typename == 0: if self.NewLine == False: sys.stdout.write('/n') sys.stdout.flush() sys.stdout.write(str + '/n') sys.stdout.flush() self.NewLine = True# 1 is "......." if typename == 1: sys.stdout.write(str) sys.stdout.flush() self.NewLine = False# 2 is "/n......" if typename == 2: if self.NewLine == False: sys.stdout.write('/n') sys.stdout.flush() sys.stdout.write(str) sys.stdout.flush() self.NewLine = False# 3 is "....../n" if typename == 3: sys.stdout.write(str + '/n') sys.stdout.flush() self.NewLine = True############################
新聞熱點(diǎn)
疑難解答
圖片精選