美文网首页
小甲鱼python29课n-m行文本读取

小甲鱼python29课n-m行文本读取

作者: Aaron_Ren | 来源:发表于2018-01-18 16:55 被阅读0次

    def recep(reception):
    '''
    reception:接收
    scope:范围
    :param reception:
    :return:
    '''
    if reception.startswith(":"):
    # 取scope[1]
    scope_start = reception.split(':')
    return 'scope_start',int(scope_start[1])
    elif reception.endswith(":"):
    # 取scope[0]
    scope_end = reception.split(':')
    return 'scope_end',int(scope_end[0])
    else:
    # 取scope[begin:end]
    scope_s_e = reception.split(':')
    return 'scope_s_e',[int(scope_s_e[0]),int(scope_s_e[1])]
    def file_defi(file,scope=[]):
    with open(file,'r') as f:
    list1=f.readlines()
    length=len(list1)
    if scope[0]=='scope_start' and scope[1]>=0 and scope[1]<=length:
    for i in range(0,scope[1]):
    print(list1[i].strip())
    elif scope[0] == 'scope_end' and scope[1]>=1 and scope[1]<=length:
    for i in range(scope[1]-1,length):
    print(list1[i].strip())
    elif scope[0]=='scope_s_e' and scope[1][0]>=0 and scope[1][1]<=length:
    for i in range(scope[1][0]-1,scope[1][1]):
    print(list1[i].strip())
    else:
    print('输入范围有误!')

    '''
    接收用户的输入
    '''

    file=input('输入要查找的文件:').strip()
    reception=(str(input('请输入读取的范围(:n,n:,n:m):'))).strip()
    temp=recep(reception)
    file_defi(file,temp)

    相关文章

      网友评论

          本文标题:小甲鱼python29课n-m行文本读取

          本文链接:https://www.haomeiwen.com/subject/jomwoxtx.html