ランダムな文字を使ってUnicode文字列を生成しようとしています。私は文字列中に印刷可能でない文字を持つことを望んでいません。 'unichr(codepoint)'関数を使用しています。私はコードポイントをUnicodeに変換し、 'unicode.encode(' utf-8 ')'を使用しています。 string.printableを使用しようとしましたが、それはASCIIのみをカバーします。どのように私は非印刷のUnicode文字をPythonで認識できますか?
0
A
答えて
0
unicodedata
ライブラリを使用できます。
import unicodedata
def strip_string(self, string):
"""Cleans a string based on a whitelist of printable unicode categories
You can find a full list of categories here:
http://www.fileformat.info/info/unicode/category/index.htm
"""
letters = ('LC', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu')
numbers = ('Nd', 'Nl', 'No')
marks = ('Mc', 'Me', 'Mn')
punctuation = ('Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po', 'Ps')
symbol = ('Sc', 'Sk', 'Sm', 'So')
space = ('Zs',)
allowed_categories = letters + numbers + marks + punctuation + symbol + space
return u''.join([ c for c in string if unicodedata.category(c) in allowed_categories ])
+0
''( 'U + 0601')を「印刷可能」と考えてみませんか? –
+0
潜在的に印刷可能であっても、依然としてフォントサポートの問題があります。ほとんどのフォントは、すべての印刷可能なUnicode文字をサポートしていません。 –
+0
@一二三ユーザーが印刷可能と考えるものはまあまあです。 U + 0601を印刷可能と見なすには、リストに 'Cf 'を追加します。 –
関連する問題
- 1. 認識できない文字の(行で)ANTLR印刷インデックス
- 2. 8086でのUnicode文字の印刷
- 3. Python 3 CGIでUnicode文字を印刷するにはどうすればよいですか?
- 4. Pythonでlikeのような文字を印刷するには
- 5. Python:バイトをどのように印刷できますか?
- 6. json文字列はどのように印刷できますか?
- 7. 文字列をそのままPythonで印刷するには
- 8. Pythonのリストはどのように列に印刷できますか?
- 9. JsonフィードをPythonでどのように印刷しますか?
- 10. Python:1行のコードでUnicode文字を印刷できますが、後で印刷しようとすると同じUnicode文字でエラーが発生するのはなぜですか?
- 11. jQueryのdatepickerはどのように印刷できますか?
- 12. 文字列がiOSデバイスのUDIDであるかどうかをどのように認識できますか?
- 13. XcodeでUnicodeを使用して文字╔(およびその他の文字)を印刷するにはどうすればよいですか?
- 14. CMB2フィールドタイプはどのように印刷できますか?
- 15. Pythonのスクリーン印刷をどのようにフィルタリングできますか?
- 16. EJS:空文字列と空文字列はどのように認識できますか?
- 17. D-language:コンソールにUnicode文字を印刷する方法は?
- 18. QtでUnicode文字を検出して印刷する
- 19. WriteConsoleWでUnicode日本語文字を印刷する
- 20. どのように配列を印刷できますか?
- 21. PythonでUnicode(非ASCII)文字を扱う方法は?
- 22. Windowsはどのように非Unicodeアプリケーションを識別しますか?
- 23. 私はどのように数字x回をリストとしてpythonで印刷しますか?
- 24. どのように私はJavascriptオブジェクトでUnicodeの文字列キーを使用できますか?
- 25. Python 2.x Unicode文字列をu'string 'として印刷しないようにするには?
- 26. NCursesでUnicodeを印刷するには?
- 27. MATLABでUnicodeのテキストや文字を印刷することはできますか?
- 28. 現在のフォントでUnicode文字がサポートされているかどうかを簡単に確認できますか?そのよう指す矢印を
- 29. vimでのUnicode印刷
- 30. Pythonの文字列から印刷できない文字を取り除く?
例外処理を使用する方法について[MCVE] – MYGz
ジャストアイデアが、何を提供?それは悪い習慣とみなされるだろうか? – spicypumpkin
印刷できない文字は何と考えていますか?どのフォントですか?上記コード印刷用 –