2011-11-13 15 views
7

文字列のリストからすべてのタイプのエスケープシーケンスを削除したいと考えています。これどうやってするの? 入力:文字列のリストからすべてのエスケープシーケンスを削除するには?

['william', 'short', '\x80', 'twitter', '\xaa', '\xe2', 'video', 'guy', 'ray'] 

出力:このような

['william', 'short', 'twitter', 'video', 'guy', 'ray'] 

http://docs.python.org/reference/lexical_analysis.html#string-literals

+1

最終的な文字列オブジェクトには、エスケープシーケンスを含む文字列リテラルの情報が含まれていません。何かがあるかどうかわからなくても、どうやってそれらを "削除"しますか? –

答えて

8

何か?

>>> from ast import literal_eval 
>>> s = r'Hello,\nworld!' 
>>> print(literal_eval("'%s'" % s)) 
Hello, 
world! 

編集:OK、それはあなたが望むものではありません。 @Sven Marnachは説明したように、文字列は実際にエスケープシーケンスを含んでいないので、あなたが望むものは一般的には行えません。それらは文字列リテラルの単なる表記です。

>>> l = ['william', 'short', '\x80', 'twitter', '\xaa', '\xe2', 'video', 'guy', 'ray'] 
>>> [word for word in l if word.isalnum()] 
['william', 'short', 'twitter', 'video', 'guy', 'ray'] 

あなたはリスト内包とstr.isalnum()を使用して、英数字していない「という言葉を」フィルタリングでき

def is_ascii(s): 
    try: 
     s.decode('ascii') 
     return True 
    except UnicodeDecodeError: 
     return False 

[s for s in ['william', 'short', '\x80', 'twitter', '\xaa', 
      '\xe2', 'video', 'guy', 'ray'] 
if is_ascii(s)] 
3

であなたのリストから、非ASCII文字を含むすべての文字列をフィルタリングすることができます数字を除外する場合は、代わりにstr.isalpha()を使用してください。

>>> l = ['william', 'short', '\x80', 'twitter', '\xaa', '\xe2', 'video', 'guy', 'ray', '456'] 
>>> [word for word in l if word.isalpha()] 
['william', 'short', 'twitter', 'video', 'guy', 'ray'] 
+1

これは、多くのアプリケーションでは良い答えですが、空白のような英数字以外の文字も斧に苦しむことに注意してください。 –

2

少なくとも、あなたが求めている範囲では、これはできません。他の人が触れているように、実行時のpythonでは、エスケープシーケンスとの違いや、エスケープシーケンスとの違いは分かりません。

例:

print ('\x61' == 'a') 

プリントTrue。したがって、Pythonスクリプトの静的解析を行わない限り、これら2つの文字列の違いを見つける方法はありません。

20

あなたが好きではないいくつかの文字を取り除くしたい場合は、あなたがそれらを取り除くためにtranslate機能を使用することができます。

>>> s="\x01\x02\x10\x13\x20\x21hello world" 
>>> print(s) 
!hello world 
>>> s 
'\x01\x02\x10\x13 !hello world' 
>>> escapes = ''.join([chr(char) for char in range(1, 32)]) 
>>> t = s.translate(None, escapes) 
>>> t 
' !hello world' 

これは、これらすべての制御文字を取り除きます:

001 1  01 SOH (start of heading) 
    002 2  02 STX (start of text) 
    003 3  03 ETX (end of text) 
    004 4  04 EOT (end of transmission) 
    005 5  05 ENQ (enquiry) 
    006 6  06 ACK (acknowledge) 
    007 7  07 BEL '\a' (bell) 
    010 8  08 BS '\b' (backspace) 
    011 9  09 HT '\t' (horizontal tab) 
    012 10 0A LF '\n' (new line) 
    013 11 0B VT '\v' (vertical tab) 
    014 12 0C FF '\f' (form feed) 
    015 13 0D CR '\r' (carriage ret) 
    016 14 0E SO (shift out) 
    017 15 0F SI (shift in) 
    020 16 10 DLE (data link escape) 
    021 17 11 DC1 (device control 1) 
    022 18 12 DC2 (device control 2) 
    023 19 13 DC3 (device control 3) 
    024 20 14 DC4 (device control 4) 
    025 21 15 NAK (negative ack.) 
    026 22 16 SYN (synchronous idle) 
    027 23 17 ETB (end of trans. blk) 
    030 24 18 CAN (cancel) 
    031 25 19 EM (end of medium) 
    032 26 1A SUB (substitute) 
    033 27 1B ESC (escape) 
    034 28 1C FS (file separator) 
    035 29 1D GS (group separator) 
    036 30 1E RS (record separator) 
    037 31 1F US (unit separator) 
+0

申し訳ありませんが、そのループは私にうんざりさせます。 @joam(2)のような文字列を返すことができます。 '' escape = '' .join([chr(char)in range(1、32)]) '' s.translate(None、escapes) ' – Rebs

+0

@AdamGriffiths、ありがとう。 – sarnold

+0

私はこの答えが最初のものよりも好きです。それはうまく動作し、非常に柔軟です。 – weefwefwqg3

0

16進数からStringに変換する際に同様の問題が発生しました。これは最終的にはPythonで動作しました 例

list_l = ['william', 'short', '\x80', 'twitter', '\xaa', '\xe2', 'video', 'guy', 'ray'] 
decode_data=[] 
for l in list_l: 
    data =l.decode('ascii', 'ignore') 
    if data != "": 
     decode_data.append(data) 

# output :[u'william', u'short', u'twitter', u'video', u'guy', u'ray'] 
関連する問題