在Python中查找特定字符串前的9个字符

2024-10-02 08:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,这里有一行“2020-08-12 13:45:04 0.86393%ContractWeber”,我需要在文本文件中“%ContractWeber”字符串之前提取9个字符。 所以,这里我的答案是“0.86393”。我不明白用Python怎么做?请帮忙

1


Tags: 字符串答案文本文件个字符contractweber
1条回答
网友
1楼 · 发布于 2024-10-02 08:17:27
line = '2020-08-12 13:45:04 0.86393 %contrastWeber'

position = line.index('%contrastWeber')

if position >= 0:
    starting = position - 9
    if starting < 0:
        starting = 0
    ending = position
    print(line[starting : ending])
else:
    print('is not found')

相关问题 更多 >

    热门问题