python實現將內容分行輸出

來源:互聯網
上載者:User
#python版一行內容分行輸出

a="aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九""""分行輸出為:abcdefghiABCDEFGHI123456789一二三四五六七八九""" print("方法一:===============")for r in range(0,4): t='' for s in range(0+r,len(a),4):  t=t+a[s] print(t) print("方法二:===============") #=_=這個方法會不會看起來比較傻?l=list(a)ta=tb=tc=td=''for r in range(0,9): for s in range(0,4):  if s==0:   ta=ta+l.pop(0)  if s==1:   tb=tb+l.pop(0)  if s==2:   tc=tc+l.pop(0)  if s==3:   td=td+l.pop(0)print(ta)print(tb)print(tc)print(td)  print("方法3:回字有N種寫法===============")import stringta=tb=tc=td=''la=string.ascii_lowercaseua=string.ascii_uppercasenb=string.digitsub="一二三四五六七八九"for s in a: if s in la:  ta=ta+s if s in ua:  tb=tb+s if s in nb:  tc=tc+s if s in ub:  td=td+sprint(ta)print(tb)print(tc)print(td) print("方法4:回字有一種叫做正則的寫法===============")import re#這正則寫法感覺不科學,暫時沒有好的想法reg=["[a-z]","[A-Z]","\d","[^\da-zA-Z]"]for s in reg:  rega=re.compile(s) s=re.findall(rega,a) print("".join(s)) """輸出:方法一:===============abcdefghiABCDEFGHI123456789一二三四五六七八九方法二:===============abcdefghiABCDEFGHI123456789一二三四五六七八九方法3:回字有N種寫法===============abcdefghiABCDEFGHI123456789一二三四五六七八九方法4:回字有一種叫做正則的寫法===============abcdefghiABCDEFGHI123456789一二三四五六七八九"""

再給大家一個讀取檔案內容並分行輸出的方法

f=open("shuju.txt","r")content=f.read()print contentfor i in content:  print if.close()f=open('shuju.txt','w')f.write(content)f.close()

好了,小夥伴們自己好好研究下吧,很有意思。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.