1

文字列から文字と空白を削除しようとしていますが\r\n、結果に表示されないようになっています。また、私が与えた正規表現を除いた結果を返す関数はありますか?


除外する必要がある私のコード\r\n

region = ",,,Central California\r\n"

#\w Matches word characters.
#\s Matches whitespace
print re.findall(r"[\w\s]+", region)

除外された出力['Central California']

出力を取得しました['Central California\r\n']


正規表現に一致しないものをすべて返します

region = ",,,Central California\r\n"

#\W Matches nonword characters.
print re.exclude_function(r"[\W]+", region)

除外された出力['Central California']


4

2 に答える 2

3

csvファイルを解析しているようです。そのためには、組み込みのPythonライブラリの使用を検討する必要があります。

末尾の改行を削除するには、str.srip()を使用できます。

すべてのセグメントのすべてをキャプチャしたい場合は、それよりもはるかに簡単なことを行うことができます。

re.findall(r',?([^,]+)(?:,|\r\n)', string)
# this regex captures anything between `,` and/or a newline

文字列で表示する:

>>> s = ",,,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['Central California']

複数のアイテムがある場合:

>>> s = ",itemA,itemB,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['itemA', 'itemB', 'Central California']

>>> s = "BASE,itemA,itemB,Central California\r\n"
>>> re.findall(r',?([^,]+)(?:,|\r\n)', s)
['BASE', 'itemA', 'itemB', 'Central California']
于 2012-12-20T09:36:05.563 に答える
1

\sが含まれ\r\nいるので、

re.findall(r"[\w\t ]+", region)

代わりに("\t"タブ文字であり" "、まあ、スペースです)。

また、正規表現に一致しなかったものをすべて返す関数が必要な場合は、単にreplace-allを実行します。

def exclude_function(regex, string):
    return re.sub(regex, "", string)
于 2012-12-20T09:33:14.813 に答える