2017-02-11 1 views
0

引用符で囲まれたテキストを示すために ">"行接頭辞を使用するテキストを折り返すために使用できるPythonモジュールまたは既存のPythonコードを探しています(下記の例を参照)。python:電子メールの慣行で引用されたテキストをラッピングするモジュールですか?

私はPython textwrapモジュールを使用して、段落のテキストを折り返すことができます。しかし、このモジュールはこの種の引用符を知らない。

私は、このテキストの折り返しを実行するルーチンをコード化する方法を知っています。それを書く方法についてのアドバイスはありません。むしろ、すでに存在し、電子メールタイプの引用されたテキストにこの種のラッピングをすでに実行できるPythonコードまたはPythonモジュールを知っている人がいるかどうかは疑問です。

私は検索していますが、私はPythonで何も見つかりませんでした。

このようなことが既に書かれている場合、私はちょうど "ホイールを再発明"したくありません。

ここでは、実行したいテキストラッピングの例を示します。私は、架空の電子メールメッセージから来て、次のテキストがあるとします。

Abc defg hijk lmnop. 

Mary had a little lamb. 
Her fleas were white as snow, 

> Now is the time for all good men to come to the aid of their party. 
> 
> The quick 
> brown fox jumped over the lazy sleeping dog. 

>> When in the Course of human 
>> events it 
>> becomes necessary for one people to dissolve the political 
>> bands 
>> which have 
>> connected them ... 
     and everywhere that Mary went, 
     her fleas were sure to go 
     ... and to reproduce. 
> What do you mean by this? 
>> with another 
>> and to assume among 
>> the powers of the earth ... 
> Doo wah diddy, diddy dum, diddy doo. 
>> Text text text text text text text text text text text text text text text text text text text text text text text text text text text. 

は結果のテキストは次のようになり、私は列52でラップしたいと仮定すると:

Abc defg hijk lmnop. 

Mary had a little lamb. Her fleas were white as 
snow, 

> Now is the time for all good men to come to the 
> aid of their party. 
> 
> The quick brown fox jumped over the lazy sleeping 
> dog. 

>> When in the Course of human events it becomes 
>> necessary for one people to dissolve the 
>> political bands which have connected them ... 
     and everywhere that Mary went, her fleas were 
     sure to go ... and to reproduce. 
> What do you mean by this? 
>> with another and to assume among the powers of 
>> the earth ... 
> Doo wah diddy, diddy dum, diddy doo. 
>> Text text text text text text text text text text 
>> text text text text text text text text text text 
>> text text text text text text text. 

すべての参照をありがとう既存のPythonコードに追加します。

「野生の中に」存在しない場合は、これを書いてコードを投稿します。

ありがとうございました。

答えて

0

このような引用符で囲まれたテキストをラップする既存のコードは見つかりませんでしたので、ここには私が書いたコードがあります。これは、とtextwrapモジュールを使用します。

最初の引用符や字下げ文字の数に基づいてコードを「段落」に分割します。次に、テキストラップを使用して、各 "段落"を各行から削除された引用符またはインデント接頭辞で囲みます。折り返しの後、接頭辞を "段落"の各行に付け直します。

私はコードを整理してもう少しエレガントにしますが、少なくともそれは正常に動作するようです。

import re 
import textwrap 
def wrapemail(text, wrap=72): 
    if not text: 
     return '' 
    prefix  = None 
    prev_prefix = None 
    paragraph = [] 
    paragraphs = [] 
    for line in text.rstrip().split('\n'): 
     line = line.rstrip() 
     m = wrapemail.qprefixpat.search(line) 
     if m: 
      prefix = wrapemail.whitepat.sub('', m.group(1)) 
      text = m.group(2) 
      if text and wrapemail.whitepat.search(text[0]): 
       prefix += text[0] 
       text = text[1:] 
     else: 
      m = wrapemail.wprefixpat.search(line) 
      if m: 
       prefix = m.group(1) 
       text = m.group(2) 
      else: 
       prefix = '' 
       text = line 
     if not text: 
      if paragraph and prev_prefix is not None: 
       paragraphs.append((prev_prefix, paragraph)) 
      paragraphs.append((prefix, [''])) 
      prev_prefix = None 
      paragraph = [] 
     elif prefix != prev_prefix: 
      if paragraph and prev_prefix is not None: 
       paragraphs.append((prev_prefix, paragraph)) 
      prev_prefix = prefix 
      paragraph = [] 
     paragraph.append(text) 
    if paragraph and prefix is not None: 
     paragraphs.append((prefix, paragraph)) 
    result = '' 
    for paragraph in paragraphs: 
     prefix = paragraph[0] 
     text = '\n'.join(paragraph[1]).rstrip() 
     wraplen = wrap - len(prefix) 
     if wraplen < 1: 
      result += '{}{}\n'.format(prefix, text) 
     elif text: 
      for line in textwrap.wrap(text, wraplen): 
       result += '{}{}\n'.format(prefix, line.rstrip()) 
     else: 
      result += '{}\n'.format(prefix) 
    return result 
wrapemail.qprefixpat = re.compile(r'^([\s>]*>)([^>]*)$') 
wrapemail.wprefixpat = re.compile(r'^(\s+)(\S.*)?$') 
wrapemail.whitepat = re.compile(r'\s') 

「ラップ」を52と指定して元のメッセージにテキストを入力すると、上で指定した出力が実際に生成されます。

これを改善したり、それを盗みます。 :)

関連する問題