1165

文字列から空白 (スペースとタブ) を削除する Python 関数はありますか?

例:\t example string\texample string

4

15 に答える 15

1702

両側の空白には次を使用しますstr.strip

s = "  \t a string example\t  "
s = s.strip()

右側の空白には次を使用しますrstrip

s = s.rstrip()

左側の空白の場合lstrip:

s = s.lstrip()

thedzが指摘しているように、次のように、これらの関数のいずれかに任意の文字を削除する引数を指定できます。

s = s.strip(' \t\n\r')

これにより、文字列の左側、右側、または両側からスペース、、、または文字\tが削除さ\nれます。\r

上記の例では、文字列の左側と右側からのみ文字列が削除されます。文字列の途中からも文字を削除したい場合は、次を試してくださいre.sub

import re
print(re.sub('[\s+]', '', s))

それは印刷されるはずです:

astringexample
于 2009-07-26T20:56:26.283 に答える
79

Pythontrimメソッドが呼び出されstripます:

str.strip() #trim
str.lstrip() #ltrim
str.rstrip() #rtrim
于 2012-02-17T10:00:00.977 に答える
23

先頭および末尾の空白の場合:

s = '   foo    \t   '
print s.strip() # prints "foo"

それ以外の場合は、正規表現が機能します。

import re
pat = re.compile(r'\s+')
s = '  \t  foo   \t   bar \t  '
print pat.sub('', s) # prints "foobar"
于 2009-07-26T20:56:06.490 に答える
20

非常にシンプルで基本的な関数を使用することもできます: str.replace()、空白とタブで動作します:

>>> whitespaces = "   abcd ef gh ijkl       "
>>> tabs = "        abcde       fgh        ijkl"

>>> print whitespaces.replace(" ", "")
abcdefghijkl
>>> print tabs.replace(" ", "")
abcdefghijkl

シンプルで簡単。

于 2014-06-11T14:18:09.313 に答える
12
#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']
于 2012-02-13T05:16:24.260 に答える
4

これらの正規表現ソリューションはまだ誰も投稿していません。

マッチング:

>>> import re
>>> p=re.compile('\\s*(.*\\S)?\\s*')

>>> m=p.match('  \t blah ')
>>> m.group(1)
'blah'

>>> m=p.match('  \tbl ah  \t ')
>>> m.group(1)
'bl ah'

>>> m=p.match('  \t  ')
>>> print m.group(1)
None

検索 (「スペースのみ」の入力ケースを別の方法で処理する必要があります):

>>> p1=re.compile('\\S.*\\S')

>>> m=p1.search('  \tblah  \t ')
>>> m.group()
'blah'

>>> m=p1.search('  \tbl ah  \t ')
>>> m.group()
'bl ah'

>>> m=p1.search('  \t  ')
>>> m.group()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'

を使用するre.subと、内部の空白が削除される可能性がありますが、これは望ましくない可能性があります。

于 2013-02-12T02:22:02.500 に答える
4

空白には、スペース、タブ、および CRLFが含まれます。したがって、使用できるエレガントでワンライナーの文字列関数はtranslateです。

' hello apple'.translate(None, ' \n\t\r')

または、徹底したい場合

import string
' hello  apple'.translate(None, string.whitespace)
于 2015-11-28T05:45:56.843 に答える
2
    something = "\t  please_     \t remove_  all_    \n\n\n\nwhitespaces\n\t  "

    something = "".join(something.split())

出力:

please_remove_all_whitespaces


Le Droid のコメントを回答に追加します。スペースで区切るには:

    something = "\t  please     \t remove  all   extra \n\n\n\nwhitespaces\n\t  "
    something = " ".join(something.split())

出力:

余分な空白をすべて削除してください

于 2015-06-19T02:58:48.990 に答える
-1

一般的に、私は次の方法を使用しています。

>>> myStr = "Hi\n Stack Over \r flow!"
>>> charList = [u"\u005Cn",u"\u005Cr",u"\u005Ct"]
>>> import re
>>> for i in charList:
        myStr = re.sub(i, r"", myStr)

>>> myStr
'Hi Stack Over  flow'

注: これは、"\n"、"\r"、および "\t" のみを削除するためのものです。余分なスペースは削除されません。

于 2015-10-02T12:35:41.163 に答える