4

str.strip()まあ、これは繰り返しに聞こえるかもしれませんが、私は, str.rstrip(),のようなすべての可能性を試しましたstr.splitline().

if str is not '' or str is not '\n':
    print str

しかし、出力に改行が入り続けます。

os.popen の結果を保存しています:

list.append(os.popen(command_arg).read())

私がするとき、私はprint list得る

['output1', '', 'output2', 'output3', '', '', '','']

私の目標は取得することです

output1
output2
output3

それ以外の

    output1
  <blank line>
    output2
    output3
 <blank line>    
 <blank line>
4

9 に答える 9

3

あなたの場合にお勧めします:

if str.strip():
    print str

それ以外の

if str is not '' or str is not '\n':
    print str

重要: 文字列が等しいかどうかのテストは、s == "..."ではなくを使用して行う必要がありますs is "..."

于 2013-06-07T09:10:04.407 に答える
0
于 2013-06-07T09:10:23.880 に答える
0

空の文字をすべて削除するには、次を使用できます。

>>> ll=['1','',''] 
>>> filter(None, ll) 
output : ['1']

これを試してください:

>>> l=['1','']
>>> l.remove('')
>>> l
['1']

またはこれを試して、文字列からすべての特殊文字を削除します。

>>>import re
>>> text = "When asked about      Modi's possible announcement as BJP's election campaign committee head, she said that she cannot conf
irm or deny the development."
>>> re.sub(r'\W+', ' ', text)
'When asked about Modi s possible announcement as BJP s election campaign committee head she said that she cannot confirm or deny the d
evelopment '
于 2013-06-07T10:07:55.413 に答える
-1

「または」の代わりに「および」を使用する

 if str is not '' and str is not '\n':
       print str
于 2013-06-07T09:12:45.830 に答える