简体   繁体   English

用反斜杠从字符串中提取数字

[英]Extract numbers from string with backslash

I need to extract the numbers from a string in the following format: '183\118\40'.我需要从以下格式的字符串中提取数字:'183\118\40'。 I tried:我试过了:

st = '183\118\40'
re.findall(r"[-+]?(?:\d*\.\d+|\d+)", st)

output: ['183', '8'] output:['183','8']

st.split('\\')

output: ['183\t8 '] output:['183\t8']

You have confused the REPRESENTATION of your string with the CONTENT of your string.您将字符串的 REPRESENTATION 与字符串的 CONTENT 混淆了。 The string '183\118\40' contains 6 characters, NONE of which are backslashes.字符串'183\118\40'包含 6 个字符,其中 NONE 是反斜杠。 The "\11" is an octal character constant. "\11" 是一个八进制字符常量。 Octal 11 is decimal 9, which is the tab character.八进制 11 是十进制 9,它是制表符。 The "\40" is also an octal character constant. "\40" 也是一个八进制字符常量。 Octal 40 is decimal 32, which is space.八进制 40 是十进制 32,即空格。

If you really want that literal string, you need one of:如果您真的想要该文字字符串,则需要以下之一:

st = '183\\118\\40'
st = r'183\118\40'

Note that this only happens because you have typed it as a Python string constant.请注意,这只是因为您将其键入为 Python 字符串常量。 If you read that line in from file, it will work just fine.如果您从文件中读取该行,它将正常工作。

st = '183\118\40'
print(st)

If you try printing this, you will see that this is the output如果你尝试打印这个,你会看到这是 output

'183\t8 '

This is not in your required format because of the backslash or escape character() used.由于使用了反斜杠或转义字符(),这不是您所需的格式。 To solve this issue, use raw strings.要解决此问题,请使用原始字符串。 To convert your string into a raw string, do this要将您的字符串转换为原始字符串,请执行此操作

st = r'183\118\40'

If you do not want to use raw strings, save it as如果您不想使用原始字符串,请将其另存为

st = '183\\118\\40'

If you try printing this, you will see that this is the output如果你尝试打印这个,你会看到这是 output

'183\\118\\40'

As you can see, the escape character has been itself escaped by using another escape character after it.如您所见,转义字符本身已通过在其后使用另一个转义字符进行转义。

Now, to get the required number values from this, use string manipulation method split() with the delimiter arguement.现在,要从中获取所需的数值,请使用带有分隔符参数的字符串操作方法 split()。 Like so,像这样,

st.split("\\")

Note that here we again escape the escape character with another backslash to set the delimiter as the literal / character.请注意,这里我们再次使用另一个反斜杠转义转义字符,以将分隔符设置为文字 / 字符。

Now,现在,

print(st)

will give you your required output, that is,会给你你需要的output,也就是说,

['183', '118', '40']

Now you are ready to further process this list of strings.现在您已准备好进一步处理此字符串列表。

If you want a list of integers instead, try如果您想要一个整数列表,请尝试

int_list = [int(x) for x in st.split('\\')]

Now visualise the output,现在可视化 output,

>>>print(int_list)
[183, 118, 40]

It is recommended to use '/xx', str = '\xx' default will be transferred by default.推荐使用'/xx',str = '\xx' default 会默认传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM