2013-08-22 36 views
9

Her satırın başına bazı karakterler eklemek istiyorum.Her satırın başına bazı karakterler ekleyin

Nasıl yapabilirim? Bunu yaptığını

:

'\n\t\t\t'.join(myStr.splitlines())

Ama mükemmel değildir ve bunu yapmak için daha iyi yolları olup olmadığını bilmek istiyorum. Orijinal olarak bir metin bloğunu otomatik olarak girmek istiyorum.

cevap

8

Bence bu oldukça güzel bir yöntem. İyileştirebildiğiniz bir şey, yönteminizin önde gelen bir yeni satırı tanıttığı ve sondaki tüm yeni satırları kaldırdığıdır. Bu olmaz:

'\t\t\t'.join(myStr.splitlines(True)) 

From the docs:

str.splitlines ([keepends]) dizede çizgilerin

geri liste, hat sınırlarında kesiliyor. Bu yöntem, satırları ayırmak için evrensel yeni satırlar yaklaşımını kullanır. Satır sonları verilen ve doğru olmadığı sürece, sonuç listesine dahil değildir. dize bir satır ile başlar sürece

Ayrıca, sen dize başında hiçbir sekme ekleyerek değildir bunu da isteyebilirsiniz: Esnek İçin

'\t\t\t'.join(('\n'+myStr.lstrip()).splitlines(True)) 
+0

Ben, o sayesinde bilmiyordum! – JeromeJ

1

Seçenek olarak, standart kitaplıkta textwrap'a bakmak isteyebilirsiniz.

Örnek:

>>> hamlet='''\ 
... To be, or not to be: that is the question: 
... Whether 'tis nobler in the mind to suffer 
... The slings and arrows of outrageous fortune, 
... Or to take arms against a sea of troubles, 
... And by opposing end them? To die: to sleep; 
... No more; and by a sleep to say we end 
... ''' 
>>> import textwrap 
>>> wrapper=textwrap.TextWrapper(initial_indent='\t', subsequent_indent='\t'*2) 
>>> print wrapper.fill(hamlet) 
    To be, or not to be: that is the question: Whether 'tis nobler in the 
     mind to suffer The slings and arrows of outrageous fortune, Or to 
     take arms against a sea of troubles, And by opposing end them? To 
     die: to sleep; No more; and by a sleep to say we end 

Sen

vb sekmeleri genişletmek, tire koymak sığdırmak için her satırı kırpabilir, sadece kolayca her satırın önündeki esnek boşluk eklemek olamayacağını görebilirsiniz

O (dolayısıyla adı) çünkü ön eklemeler çok uzun haline satırları kaydırılır:

>>> wrapper=textwrap.TextWrapper(initial_indent='\t'*3, 
... subsequent_indent='\t'*4, width=40) 
>>> print wrapper.fill(hamlet) 
      To be, or not to be: that is the 
       question: Whether 'tis nobler in the 
       mind to suffer The slings and arrows 
       of outrageous fortune, Or to take 
       arms against a sea of troubles, And 
       by opposing end them? To die: to 
       sleep; No more; and by a sleep to 
       say we end 

Çok esnek ve kullanışlı.

Düzenleme

sadece aynı sonları sınır çizmek splitlines ile textwrap birleştirmek, textwrap ile metinde satır sonları anlamını tutmak isterseniz. asılı girinti

Örnek:

import textwrap 

hamlet='''\ 
Hamlet: In the secret parts of Fortune? O, most true! She is a strumpet. What's the news? 
Rosencrantz: None, my lord, but that the world's grown honest. 
Hamlet: Then is doomsday near.''' 

wrapper=textwrap.TextWrapper(initial_indent='\t'*1, 
          subsequent_indent='\t'*3, 
          width=30) 

for para in hamlet.splitlines(): 
    print wrapper.fill(para) 
    print 

baskılar

Hamlet: In the secret parts 
     of Fortune? O, most true! 
     She is a strumpet. What's 
     the news? 

Rosencrantz: None, my lord, 
     but that the world's grown 
     honest. 

Hamlet: Then is doomsday 
     near. 
+0

Belki de, bir şekilde, yeni satır tipi ayırıcıyı (\ n, \ r \ n veya \ r, eğer haklıysam gibi) str.splitlines (True) 'gibi tutuyor muyuz?Bu modüle daha yakından bakacağım ama bu düzgün görünüyor! – JeromeJ

+0

(… (2) ya da belki de bu tamamen işe yaramaz ve sadece "\ n" her yerde kullanabilir miyim?) – JeromeJ

+0

@JeromeJ: Neden işe yaramaz diyorsun? Ne textwrap yaptığı [hiç de önemsiz değil] (http://hg.python.org/cpython/file/2.7/Lib/textwrap.py). Kelime işlemcide kelime sarmayı düşünün. Satırlarınızın önüne 3 sekme eklerseniz, satırda gösterilen alanda çok uzun süre ne olur? Bu kütüphane, her satırın başına yer açmanıza ve o alanın çok geniş olmasını sağlarsa kalan tüm satırları kaydırmanıza olanak tanır. Bu işe yaramaz nasıl? – dawg

İlgili konular