鱼C论坛

 找回密码
 立即注册
查看: 10899|回复: 5

用split分割了字符串后,取出时显示:ValueError: need more than 0 values to unpack

[复制链接]
发表于 2015-8-4 16:28:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
文件是可以正常打开的....也读取了数据,但就是每次都显示ValueError: need more than 0 values to unpack
代码如下:
  1. stf_dict = {}
  2. f = file('stu_info.txt')
  3. for line in f:
  4.     print line
  5.     stf_id, stf_name, stf_mail, stf_company, stf_title, stf_phone = line.split()
  6.     print line.split()
  7.     stf_dict[stf_id] = [stf_name, stf_mail, stf_company, stf_title, stf_phone]
复制代码


文件stu_info.txt里的内容是:
stu1101 mingjia.xu 275896019@qq.com  263 SystemAdmin 18810404260

stu1102 Yangjiansong jason@s156.com A8music SystemAdmin 13601247960

stu1103  zouxinkai zouxinkai_2006@126.com  jishubu systemadmin  1861214111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-8-4 17:28:50 | 显示全部楼层
try:
  1. stf_dict = {}
  2. f = file('stu_info.txt')
  3. for line in f:
  4.     print line
  5.     if len(line.split()) >= 6:
  6.         stf_id, stf_name, stf_mail, stf_company, stf_title, stf_phone = line.split()
  7.         print line.split()
  8.         stf_dict[stf_id] = [stf_name, stf_mail, stf_company, stf_title, stf_phone]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-4 17:42:51 | 显示全部楼层

能问下为什么需要一个判断吗??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-4 18:30:47 | 显示全部楼层
出错代码语句:
  1. root@localhost:~# python 2.py
  2. tu1101 mingjia.xu 275896019@qq.com  263 SystemAdmin 18810404260

  3. ['tu1101', 'mingjia.xu', '275896019@qq.com', '263', 'SystemAdmin', '18810404260']


  4. Traceback (most recent call last):
  5.   File "2.py", line 5, in <module>
  6.     stf_id, stf_name, stf_mail, stf_company, stf_title, stf_phone = line.split()  -->出错代码
  7. ValueError: need more than 0 values to unpack
复制代码


当读到第二行的时候,line.split()是个空列表,报错,把它过滤掉即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-4 18:51:19 | 显示全部楼层

哦,谢谢啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-8-5 10:44:26 | 显示全部楼层

:lol:一起加油!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-17 02:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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