0

次の文字列で、すべての特殊文字を削除する方法

str1="0Cell Phones: Smartphone,\x02\x05\x0e\x02\x05Mobile Phone - Best Buy13AT&T Wireless: AT&T Phones & Cell Phone Plans - Best Buy4\x02\x05\x0e\x02\x05Cell Phones: Smartphone, Mobile Phone - Best Buy1\x02\x05\x0e\x02\x05AT&T Wireless: AT&T Phones & Cell Phone Plans - Best Buy"
4

1 に答える 1

2

使用ord():

In [35]: str1="0Cell Phones: Smartphone,\x02\x05\x0e\x02\x05Mobile Phone - Best Buy13AT&T Wireless: AT&T Phones & Cell Phone Plans - Best Buy4\x02\x05\x0e\x02\x05Cell Phones: Smartphone, Mobile Phone - Best Buy1\x02\x05\x0e\x02\x05AT&T Wireless: AT&T Phones & Cell Phone Plans - Best Buy"

In [36]: "".join(x for x in str1 if 31 < ord(x) <127)

Out[36]: '0Cell Phones: Smartphone,Mobile Phone - Best Buy13AT&T Wireless: AT&T Phones & Cell Phone Plans - Best Buy4Cell Phones: Smartphone, Mobile Phone - Best Buy1AT&T Wireless: AT&T Phones & Cell Phone Plans - Best Buy'

最初の 32 文字は非印刷文字で、この範囲にはなどが含まれreturn、印刷可能な ASCII 文字です。backspace32-127

128-255: 拡張アスキー

ord()は、単一の文字に対してこの整数値を返し、 でフィルタリングできます31 < ord(x) <127

ここに画像の説明を入力

于 2013-01-10T11:27:53.203 に答える