如何在Python中的非打印ascii字符处分割行
如何在Python中的非打印ascii字符处分割一行(例如长减号十六进制0x97,八进制227)? 我不需要角色本身。后面的信息将被保存为变量。
How can I split a line in Python at a non-printing ascii character (such as the long minus sign hex 0x97 , Octal 227)?
I won't need the character itself. The information after it will be saved as a variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
re.split
。调整模式以仅包含您想要保留的字符。
另请参阅:stripping-non-printable-characters-from-a -string-in-python
示例(带长减号):
或者,与 unicode 相同:
You can use
re.split
.Adjust the pattern to only include the characters you want to keep.
See also: stripping-non-printable-characters-from-a-string-in-python
Example (w/ the long minus):
Or, the same with unicode:
或者
如果
your_input_string
不包含'\x97'
,则your_result
将为空。如果your_input_string
包含多个'\x97'
个字符,your_result
将包含第一个'\ x97'
字符,包括其他'\x97'
字符。or
If
your_input_string
does not contain a'\x97'
, thenyour_result
will be empty. Ifyour_input_string
contains multiple'\x97'
characters,your_result
will contain everything after the first'\x97'
character, including other'\x97'
characters.只需使用 string/unicode split 方法(他们并不真正关心您拆分的字符串(除了它是一个常量之外。如果您想使用正则表达式,请使用 re.split)
要获取拆分字符串,请转义它就像其他人所展示的那样
"\x97"
或
使用 chr(0x97) 表示字符串 (0-255) 或使用 unichr(0x97) 表示 unicode,
示例如下
Just use the string/unicode split method (They don't really care about the string you split upon (other than it is a constant. If you want to use a Regex then use re.split)
To get the split string either escape it like the other people have shown
"\x97"
or
use chr(0x97) for strings (0-255) or unichr(0x97) for unicode
so an example would be