如何从字符串中提取以特定字母/字符开头的子字符串?
例如,如果我有以下字符串:如何从字符串中提取以特定字母/字符开头的子字符串?
fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."
我想提取的价格像这样的列表:
['$3.00', '$2.00', '$10000']
到目前为止,我已经做到了这一点:
def extract_prices(s):
prices = []
for i in range(len(s)):
if s[i] == '$':
prices.append(s[i], s.find(' '))
我觉得最后一行是给我的问题。我不知道如何获得价格之后的空间索引,以及如何在该空间停止索引。
任何提示?感谢您阅读本文!对不起,如果我的解释不清楚。
分割字符串,并寻找美元符号:
>>> fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."
>>> result = [item.strip(',.!?') for item in fruits.split() if '$' in item]
>>> result
['$3.00', '$2.00', '$10000']
记住,从每个项目剥离标点符号。
您可以使用正则表达式:
>>> fruits = "The apples are $3.00, the oranges are $2.00, and the grapes are $10000."
>>> re.findall(r'(\$[\d.]+)', fruits)
['$3.00', '$2.00', '$10000.']
或者,如果你想更具体的,只包括.
如果有数字:
>>> re.findall(r'(\$\d+(?:\.\d+)?)', fruits)
['$3.00', '$2.00', '$10000']
请注意,最后一个元素有一个无关的句点。 – TigerhawkT3
可能是无关的或模糊的! – dawg
如果是......而葡萄是$ 10000.00,那怎么办?“'? – TigerhawkT3
使用下面的正则表达式:
re.findall('\$\d+\.?\d+', fruits)
输出:
>>> re.findall('\$\d+\.?\d+', fruits)
['$3.00', '$2.00', '$10000']
请注意,由于'.'的原因,它也会匹配'$ 10/1'等东西。 – TigerhawkT3
@ TigerhawkT3编辑,谢谢:) – ettanany
如果只想调整原代码,使用
if s[i] == '$':
lastPos = s.find(',', i)
if lastPos == -1:
lastPos = len(s)
prices.append(s[i:lastPos])
你的线条,而不是
if s[i] == '$':
prices.append(s[i], s.find(' '))
看看正则表达式https://docs.python.org/3 /library/re.html模块 – pythad
对不起,这会有帮助吗?这有点长,我不知道从哪里开始:o谢谢你的回复! – Katerina
正则表达式对于字符串解析非常有用。考虑看看他们的教程 – qxz