0

文字列を抽出するより良い方法があります:

  'Found 1 items\ndrwxr-xr-x   - hadoop supergroup          0 2013-02-16 13:21 /user/hadoop/wiki\n'

すべての文字列は次のようになります。

  'Found **n** items\n**permissions**   - **username** **group**          **notsurewhatthisis** **date** **time** **folders(or file)**\n'

今..私はそれを次のように分割しています:

line = line.split()
num_items = int(line[1])
permissions = line[3]

等..

したがって、基本的にこれは簡単な解決策です..

これを行う「python」方法があるかどうかを確認しようとしています。

4

1 に答える 1

2
ss = ('Found 1 items\n'
      "drwxr-xr-x   - hadoop supergroup          "
      '0 2013-02-16 13:21 /user/hadoop/wiki\n')

('Found **n** items\n'
 '**permissions**   - **username** **group**          '
 '**notsurewhatthisis** **date** **time** **folders(or file)**\n')

import re

r = re.compile('Found +(\d+) +items *\n *(.+?) *- ')

print r.search(ss).groups()

ssは文字列
'Found +(\d+) +items *\n *(.+?) *- 'です は正規表現を作成するためのパターンとして使用される文字列です オブジェクト
rは正規表現です、文字列ではないオブジェクトです

于 2013-02-16T23:04:19.470 に答える