用Python取出两个特殊字符中间的字符串

我们要使用ID建表,需要取出100%的ID,ID前为’_’,后为‘.’。

ID即为图中1275和1655

cd2.txt

用Python取出两个特殊字符中间的字符串_第1张图片

cd3.txt

在这里插入图片描述

with open('cd2.txt', 'r') as f:
    lines = f.readlines()
with open('cd3.txt', 'w') as f_w:
    i = 0
    length = len(lines)
    while i < length:
        print('###', i)
        if(lines[i][0] == '>'):
            f_w.write(lines[i])
            print(i,'***',lines[i])
            i = i + 1
            continue
        while lines[i][0] != '>':
            print(i,'***',lines[i])
            begin = lines[i].find('_')
            end = lines[i].find('.')
            f_w.write(lines[i][begin + 1:end])
            f_w.write('\n')
            print(i,'***',lines[i][begin + 1:end])
            i = i + 1
        f_w.write('\n')

你可能感兴趣的:(Python,python,列表)