找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2751|回复: 2

[求助] python进行推导列表,santize函数的问题

1

主题

3

帖子

3

积分

贫民

积分
3
helloiot 发表于 2017-7-25 11:03:54 | 显示全部楼层 |阅读模式
本帖最后由 helloiot 于 2017-7-25 11:19 编辑

主要对照HeadFirst里面的代码敲的,对列表进行转换之后然后排序,但是函数调用那里一直出现问题,但是对函数
调用那里抛出异常可以转换第一个列表的第0个元素,求大神帮解答一下!
完整代码
#####################################################################
#encoding utf-8
"""这个函数用来对时间字符串进行格式转换 """
def sanitize(time_string):
    if '-' in time_string:
        splitter='-'
    elif '.' in time_string:
        splitter=':'
    else:  #如果字符串不需要清理,直接返回
        return(time_string)
    (mins,secs)=time_string.split(splitter)
    #用分隔符splitter分隔字符后分别存入mins和secs中     
    return (mins +'.'+secs)

with open('james.txt' )as jaf:
    data=jaf.readline()
james=data.strip().split(',')

with open('julie.txt' )as juf:
    data=juf.readline()
julie=data.strip().split(',')

with open('mikey.txt' )as mif:
    data=mif.readline()
mikey=data.strip().split(',')

with open('sarah.txt' )as saf:
    data=saf.readline()
sarah=data.strip().split(',')

clean_james=[]
clean_julie=[]
clean_mikey=[]
clean_sarah=[]
try:
    for each_t in james:
        clean_james.append(sanitize(each_t))
    for each_t in julie:
        clean_julie.append(sanitize(each_t))
    for each_t in mikey:
        clean_mikey.append(sanitize(each_t))
    for each_t in sarah:
        clean_sarah.append(sanitize(each_t))
except ValueError as err:
    print('file error' +str(err))
print( sorted(clean_james) )
print( sorted(clean_julie) )
print( sorted(clean_mikey) )
print( sorted(clean_sarah) )
###################################################################
错误信息:


file errorneed more than 1 value to unpack
['2.24', '2:21', '2:34']
[]
[]
[]



回复

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
helloiot  楼主| 发表于 2017-7-25 11:20:15 | 显示全部楼层
看了好久 不知道哪里出现错误了
回复 支持 反对

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
helloiot  楼主| 发表于 2017-7-25 11:27:40 | 显示全部楼层
解决了,原来是函数定义elif那里粗心大意把‘:’写成了‘.’
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表