推断指定初始值和最终值的子字符串
extrapolate substring specifying initial and final values
我想从这个字符串中推断数字部分 (379022420):
'../input/predict-volcanic-eruptions-ingv-oe/train/379022420.csv'
我想指定初始值和最终值,即
start = train/
end = .csv
这是因为我需要将它用于其他几个字符串,我想通过循环来完成。
也许您可以使用 re
模块搜索字符串:
import re
strings = [
'../input/predict-volcanic-eruptions-ingv-oe/train/379022420.csv'
# ... other strings
]
pattern = re.compile(r'(?<=train/)(\d+)(?=\.csv)')
for s in strings:
m = pattern.search(s)
if m:
print(m.group(1))
打印:
379022420
你应该使用.split()
函数
textList = [...]
for i in textList:
i.split('train/')
desiredValue = i[1].split(".")
desiredValue = desiredValue[0]
我想从这个字符串中推断数字部分 (379022420):
'../input/predict-volcanic-eruptions-ingv-oe/train/379022420.csv'
我想指定初始值和最终值,即
start = train/
end = .csv
这是因为我需要将它用于其他几个字符串,我想通过循环来完成。
也许您可以使用 re
模块搜索字符串:
import re
strings = [
'../input/predict-volcanic-eruptions-ingv-oe/train/379022420.csv'
# ... other strings
]
pattern = re.compile(r'(?<=train/)(\d+)(?=\.csv)')
for s in strings:
m = pattern.search(s)
if m:
print(m.group(1))
打印:
379022420
你应该使用.split()
函数
textList = [...]
for i in textList:
i.split('train/')
desiredValue = i[1].split(".")
desiredValue = desiredValue[0]