您的位置:首页 > 其它

coercing to Unicode错误的一个解决办法

2015-06-29 17:06 316 查看
今天调python代码,出错代码

temp_min = weather_data[time_index]['low'],
        temp_max = weather_data[time_index]['high']
        description = weather_data[time_index]['text'].replace('/', u'转')
        wind_deg = weather_data[time_index]['wind']
        response = city_name + time_name + description + u',' + wind_deg + u',' + u'最高温度' + temp_max+ u'>摄氏度,' + u'最低温度' + temp_min + u'摄氏度'


最后一行出了这么个报错:

coercing to Unicode: need string or buffer, tuple found

怎么会有tuple冒出来?看着+操作符两边都是字符串啊!
百思不得其解,最后突然想起+操作符会抛出异常,改用%操作符输出

response = u'%s%s%s,%s,最高温度%s摄氏度,最低温度%s摄氏度'%(city_name, time_name, description, wind_deg, temp_max, temp_min)


这次就没有异常抛出了,输出

西安今天小雨转阴,微风小于3级,最高温度27摄氏度,最低温度(u'22',)摄氏度


最低气温怎么会多出个括弧?原来这就是tuple的来源!temp_min语句的末尾多了个逗号,python语言自动将返回值变为tuple类型!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: