2013-03-25 1 views
19

현재 이메일 텍스트를 처리하기 위해 Python imaplib을 사용하고 있습니다.IMAP 이메일 텍스트에서 등호 '='기호를 이해하는 방법은 무엇입니까?

나는 fetch 명령을 사용하여 GMail 서버에서 원시 데이터 전자 메일을 가져옵니다. 그러나, 나는 정말로 어려운 것을 발견했다 - 등호 = '. 정상적인 등호가 아니라 특별한 기호입니다. 예를 들어

:

  1. '='가끔 텍스트 행의 끝에서 하이픈 마크 역할 :

    Depending upon your module selections, course lecturers may also contact yo= 
    u with preparatory work over the next few weeks. It would be wise to start = 
    reviewing the preparatory reading lists provided on the module syllabi now = 
    
  2. 때때로, 그것은 % '와 유사한 탈출 마크 역할 '예를 들어 :

    a=20ba<SPACE>b
    =46rom here 실제로 행동입니다 ually From here

이상한 표기법에 대해서는 완전히 혼란 스럽습니다. GMail은 앱에서 올바르게 처리 할 수 ​​있기 때문에이를 처리하기위한 지침이 있어야한다고 생각합니다.

이것은 '%'처럼 인코딩되는 것과 관련하여 HTML 인코딩과 관련이 있음을 알 수 있습니다. 하지만 문제는 IMAP 응답에서 얻은 것은 '='기호가 포함 된 문자열이라는 것입니다. 어떻게해야합니까? 정규 표현식을 사용하고 있습니까?

+4

원하는 용어는 "quoted-printable"이며 해당 서식의 이름이며 메시지의 MIME 헤더에 기록해야합니다. 인터넷 검색을 통해 필요한 모든 정보를 얻을 수 있습니다. – kindall

+0

@kindall이 키워드 주셔서 감사합니다! 나는 그것을 조사 할 것이다 – JXITC

답변

19

간단히 말해서 줄 끝의 등호는 부드러운 줄 바꿈을 나타냅니다. 두 개의 16 진수 문자 (0-9, A-F) 뒤에 오는 등호는 단일 옥텟 (바이트)을 인코딩합니다.

이 인코딩 체계는 "quoted printable"이라고하며, 섹션 6.7의 RFC 2045에 정의되어 있습니다. 특히 항목 (1)과 (5)를 참조하십시오.

6.7. Quoted-Printable Content-Transfer-Encoding 

    The Quoted-Printable encoding is intended to represent data that 
    largely consists of octets that correspond to printable characters in 
    the US-ASCII character set. It encodes the data in such a way that 
    the resulting octets are unlikely to be modified by mail transport. 
    If the data being encoded are mostly US-ASCII text, the encoded form 
    of the data remains largely recognizable by humans. A body which is 
    entirely US-ASCII may also be encoded in Quoted-Printable to ensure 
    the integrity of the data should the message pass through a 
    character-translating, and/or line-wrapping gateway. 

    In this encoding, octets are to be represented as determined by the 
    following rules: 

    (1) (General 8bit representation) Any octet, except a CR or 
      LF that is part of a CRLF line break of the canonical 
      (standard) form of the data being encoded, may be 
      represented by an "=" followed by a two digit 
      hexadecimal representation of the octet's value. The 
      digits of the hexadecimal alphabet, for this purpose, 
      are "ABCDEF". Uppercase letters must be 
      used; lowercase letters are not allowed. Thus, for 
      example, the decimal value 12 (US-ASCII form feed) can 
      be represented by "=0C", and the decimal value 61 (US- 
      ASCII EQUAL SIGN) can be represented by "=3D". This 
      rule must be followed except when the following rules 
      allow an alternative encoding. 

    (2) (Literal representation) Octets with decimal values of 
      33 through 60 inclusive, and 62 through 126, inclusive, 
      MAY be represented as the US-ASCII characters which 
      correspond to those octets (EXCLAMATION POINT through 
      LESS THAN, and GREATER THAN through TILDE, 
      respectively). 

    (3) (White Space) Octets with values of 9 and 32 MAY be 
      represented as US-ASCII TAB (HT) and SPACE characters, 

      respectively, but MUST NOT be so represented at the end 
      of an encoded line. Any TAB (HT) or SPACE characters 
      on an encoded line MUST thus be followed on that line 
      by a printable character. In particular, an "=" at the 
      end of an encoded line, indicating a soft line break 
      (see rule #5) may follow one or more TAB (HT) or SPACE 
      characters. It follows that an octet with decimal 
      value 9 or 32 appearing at the end of an encoded line 
      must be represented according to Rule #1. This rule is 
      necessary because some MTAs (Message Transport Agents, 
      programs which transport messages from one user to 
      another, or perform a portion of such transfers) are 
      known to pad lines of text with SPACEs, and others are 
      known to remove "white space" characters from the end 
      of a line. Therefore, when decoding a Quoted-Printable 
      body, any trailing white space on a line must be 
      deleted, as it will necessarily have been added by 
      intermediate transport agents. 

    (4) (Line Breaks) A line break in a text body, represented 
      as a CRLF sequence in the text canonical form, must be 
      represented by a (RFC 822) line break, which is also a 
      CRLF sequence, in the Quoted-Printable encoding. Since 
      the canonical representation of media types other than 
      text do not generally include the representation of 
      line breaks as CRLF sequences, no hard line breaks 
      (i.e. line breaks that are intended to be meaningful 
      and to be displayed to the user) can occur in the 
      quoted-printable encoding of such types. Sequences 
      like "=0D", "=0A", "=0A=0D" and "=0D=0A" will routinely 
      appear in non-text data represented in quoted- 
      printable, of course. 

      Note that many implementations may elect to encode the 
      local representation of various content types directly 
      rather than converting to canonical form first, 
      encoding, and then converting back to local 
      representation. In particular, this may apply to plain 
      text material on systems that use newline conventions 
      other than a CRLF terminator sequence. Such an 
      implementation optimization is permissible, but only 
      when the combined canonicalization-encoding step is 
      equivalent to performing the three steps separately. 

    (5) (Soft Line Breaks) The Quoted-Printable encoding 
      REQUIRES that encoded lines be no more than 76 
      characters long. If longer lines are to be encoded 
      with the Quoted-Printable encoding, "soft" line breaks 
      must be used. An equal sign as the last character on a 
      encoded line indicates such a non-significant ("soft") 
      line break in the encoded text. 
+0

해답을 많이 sooo 고맙다 !! 그래서 그것을 처리하는 가장 좋은 방법은 무엇입니까? 이 특별한 것들을 잡기 위해'= $'와'= [0-9A-Z] {2,2}'와 같은 정규 표현식을 사용해야합니까? – JXITC

+5

당신은 그것을 다루기 위해'quopri' 모듈을 사용해야합니다. http://docs.python.org/2/library/quopri.html – kindall

+0

@kindall 멋지다! 너 내가 원하는 걸 정확히 알지. :) – JXITC

관련 문제