<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Forum Bauen und Umwelt - Zeilenumbruch langer Zeilen in Python</title>
<link>https://bauforum.wirklichewelt.de/</link>
<description>Das Bauforum</description>
<language>de</language>
<item>
<title>Zeilenumbruch langer Zeilen in Python</title>
<content:encoded><![CDATA[<p>Falls jemand zufällig gerade eine Python-Funktion braucht, um längere Texte auszugeben, ohne dabei eine bestimmte Zeilenlänge zu überschreiten und dadurch Wörter am Fensterrand entzweizureißen:</p>
<pre class="python" style="font-family:monospace;"><ol><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">def</span> zeilenumbruch<span style="color: black;">&#40;</span>s<span style="color: #000;">,</span> l<span style="color: #000;">=</span><span style="color: #008;">79</span><span style="color: black;">&#41;</span>:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    aus <span style="color: #000;">=</span> <span style="color: #080;">&quot;&quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">while</span> <span style="color: #808;">len</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span> <span style="color: #000;">&gt;</span> l:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        p <span style="color: #000;">=</span> l-<span style="color: #008;">1</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;">while</span> p <span style="color: #000;">&gt;</span> <span style="color: #008;">0</span> <span style="color: #ff7700;">and</span> s<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span> <span style="color: #ff7700;">not</span> <span style="color: #ff7700;">in</span> <span style="color: #080;">&quot; -&quot;</span>:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            p -<span style="color: #000;">=</span> <span style="color: #008;">1</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;">if</span> p <span style="color: #000;">==</span> <span style="color: #008;">0</span>:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            p <span style="color: #000;">=</span> l</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        aus +<span style="color: #000;">=</span> s<span style="color: black;">&#91;</span>:p+<span style="color: #008;">1</span><span style="color: black;">&#93;</span> + <span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        s <span style="color: #000;">=</span> s<span style="color: black;">&#91;</span>p+<span style="color: #008;">1</span>:<span style="color: black;">&#93;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">return</span> aus+s</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">bt <span style="color: #000;">=</span> <span style="color: black;">&#40;</span><span style="color: #080;">&quot;Er hörte leise Schritte hinter sich. Das bedeutete nichts Gutes. &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;Wer würde ihm schon folgen, spät in der Nacht und dazu noch in dieser &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;engen Gasse mitten im übel beleumundeten Hafenviertel? Gerade jetzt, wo &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;er das Ding seines Lebens gedreht hatte und mit der Beute verschwinden &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;wollte! Hatte einer seiner zahllosen Kollegen dieselbe Idee gehabt, ihn &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;beobachtet und abgewartet, um ihn nun um die Früchte seiner Arbeit zu &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;erleichtern? Oder gehörten die Schritte hinter ihm zu einem der &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;unzähligen Gesetzeshüter dieser Stadt, und die stählerne Acht um seine &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;Handgelenke würde gleich zuschnappen? Er konnte die Aufforderung stehen &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;zu bleiben schon hören. Gehetzt sah er sich um. Plötzlich erblickte er &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;den schmalen Durchgang. Blitzartig drehte er sich nach rechts und &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;verschwand zwischen den beiden Gebäuden. Beinahe wäre er dabei über den &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;umgestürzten Mülleimer gefallen, der mitten im Weg lag. Er versuchte, &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;sich in der Dunkelheit seinen Weg zu ertasten und erstarrte: &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;Anscheinend gab es keinen anderen Ausweg aus diesem kleinen Hof als den &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;Durchgang, durch den er gekommen war. Die Schritte wurden lauter und &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;lauter, er sah eine dunkle Gestalt um die Ecke biegen. Fieberhaft &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;irrten seine Augen durch die nächtliche Dunkelheit und suchten einen &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;Ausweg. War jetzt wirklich alles vorbei, waren alle Mühe und alle &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;Vorbereitungen umsonst? Er presste sich ganz eng an die Wand hinter &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;ihm und hoffte, der Verfolger würde ihn übersehen, als plötzlich neben &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;ihm mit kaum wahrnehmbarem Quietschen eine Tür im nächtlichen Wind hin &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;und her schwang. Könnte dieses der flehentlich herbeigesehnte Ausweg &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;aus seinem Dilemma sein? Langsam bewegte er sich auf die offene Tür &quot;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;zu, immer dicht an die Mauer gepresst. Würde diese Tür seine Rettung &quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">     <span style="color: #080;">&quot;werden?&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">ut <span style="color: #000;">=</span> zeilenumbruch<span style="color: black;">&#40;</span>bt<span style="color: #000;">,</span> <span style="color: #008;">32</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808;">print</span><span style="color: black;">&#40;</span>ut<span style="color: black;">&#41;</span></div></li></ol></pre><p>Die Funktion sucht in Zeilen, die länger als die erlaubte Länge sind, nach dem innerhalb der erlaubten Zeilenlänge am weitesten rechts stehenden Leerzeichen oder Minuszeichen. Hier wird die Zeile getrennt. Der Anfang wandert, um ein Zeilenumbruchzeichen &quot;\n&quot; ergänzt, in die auszugebende Zeichenkette, das Ende wird wieder so wie zuvor untersucht.</p>
<p>Wer lieber fertige Module anstelle eigener Funktionen verwendet, sollte mal einen Blick auf <a href="https://docs.python.org/3/library/textwrap.html" target="_blank" rel=\"noopener\">das Textwrap-Modul</a> werfen. Der Zeilenumbruch-Funktion oben entspricht dabei in etwa die Funktion textwrap.fill().</p>
<p>Der spannende Blindtext entstammt übrigens dem Programm <a href="https://de.libreoffice.org/download/libreoffice-still/" target="_blank" rel=\"noopener\">Libreoffice Writer</a>.</p>
]]></content:encoded>
<link>https://bauforum.wirklichewelt.de/index.php?id=11578</link>
<guid>https://bauforum.wirklichewelt.de/index.php?id=11578</guid>
<pubDate>Thu, 25 Jan 2018 16:08:34 +0000</pubDate>
<category>Software</category><dc:creator>Martin Vogel</dc:creator>
</item>
</channel>
</rss>
