文字列から空白 (スペースとタブ) を削除する Python 関数はありますか?
例:\t example string\t
→example string
文字列から空白 (スペースとタブ) を削除する Python 関数はありますか?
例:\t example string\t
→example string
両側の空白には次を使用します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
Pythontrim
メソッドが呼び出されstrip
ます:
str.strip() #trim
str.lstrip() #ltrim
str.rstrip() #rtrim
先頭および末尾の空白の場合:
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"
非常にシンプルで基本的な関数を使用することもできます: str.replace()、空白とタブで動作します:
>>> whitespaces = " abcd ef gh ijkl "
>>> tabs = " abcde fgh ijkl"
>>> print whitespaces.replace(" ", "")
abcdefghijkl
>>> print tabs.replace(" ", "")
abcdefghijkl
シンプルで簡単。
#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 ']
これらの正規表現ソリューションはまだ誰も投稿していません。
マッチング:
>>> 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
と、内部の空白が削除される可能性がありますが、これは望ましくない可能性があります。
空白には、スペース、タブ、および CRLFが含まれます。したがって、使用できるエレガントでワンライナーの文字列関数はtranslateです。
' hello apple'.translate(None, ' \n\t\r')
または、徹底したい場合
import string
' hello apple'.translate(None, string.whitespace)
something = "\t please_ \t remove_ all_ \n\n\n\nwhitespaces\n\t "
something = "".join(something.split())
出力:
please_remove_all_whitespaces
something = "\t please \t remove all extra \n\n\n\nwhitespaces\n\t "
something = " ".join(something.split())
出力:
余分な空白をすべて削除してください
一般的に、私は次の方法を使用しています。
>>> 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" のみを削除するためのものです。余分なスペースは削除されません。