免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2223 | 回复: 4
打印 上一主题 下一主题

刚学python求助python中的import问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-19 11:02 |只看该作者 |倒序浏览
talk is cheap, see the code

nester.py:
  1. import sys

  2. """一个简单的用于输出嵌套列表的函数
  3. the_list: 输出的列表
  4. level:    每层缩进的TAB数量
  5. stream:   输出的文件默认是标准输出"""

  6. def print_lol(the_list, indent, level=0, stream=sys.stdout):
  7.         for item in the_list:
  8.                 if isinstance(item, list):
  9.                         print_lol(item, indent, level+1, stream)
  10.                 else:
  11.                         if indent:
  12.                                 for num in range(level):
  13.                                         print("\t", file=stream, end='')
  14.                         print(item, file=stream)
复制代码
sketch.py:
  1. import nester

  2. man = []
  3. other = []

  4. try :
  5.         data = open('sketch.txt')

  6.         for each_line in data :
  7.                 try :
  8.                         (role, line_spoken) = each_line.split(":", 1)
  9.                         line_spoken = line_spoken.strip()
  10.                         if role == 'Man' :
  11.                                 man.append(line_spoken)
  12.                         elif role == 'Other Man' :
  13.                                 other.append(line_spoken)

  14.                 except ValueError :
  15.                         pass
  16.         data.close()

  17. except IOError:
  18.         print('The datafile is missing!')
  19. try :
  20.         with open("man.out", "w") as out_man, open("other_man.out", "w") as out_other :
  21.                 print_lol(man, False, 0, out_man)
  22.                 print_lol(other, False, 0, out_other)

  23. except IOError as err :
  24.         print("file error: " + str(err))
复制代码
问题出在sketch.py中的import nester那行。
我在idle编辑器中F5运行nester.py,一切正常,在python shell中输入import nester正常,
并且可以调用nester模块中的函数print_lol。
但是,当我运行完nester.py紧接着运行sketch.py的时候会出现错误。如下:
  1. Traceback (most recent call last):
  2.   File "/home/ptdzm/code/python_lib/pytest/chapter3/sketch.py", line 1, in <module>
  3.     import nester
  4.   File "/home/ptdzm/code/python_lib/nester.py", line 6
  5.     """the_list: 是要显示的列表可以是嵌套列表
  6.    level:    是需要缩进的层数,每当遇到嵌套列表
  7.              的时候就缩进level个制表符"""
  8.                               
  9.                               
  10.                                ^
  11. IndentationError: expected an indented block
复制代码
请问各位这是什么问题?感谢任何形式的帮助~

论坛徽章:
0
2 [报告]
发表于 2012-12-19 11:19 |只看该作者
IndentationError: expected an indented block
表示是缩进错误。
Python中通过缩进,决定代码之间的逻辑关系。这个你应该知道吧?

所以,再去检查一下,你那出错的代码的位置,是否有多余的Tab,空格等。

关于Tab,空格等,正常肯定是看不到的。
可以使用Notepad++等工具去查看。
相关内容可参考:
Notepad++支持显示回车符,换行符,TAB键,行首,行尾等特殊字符

论坛徽章:
0
3 [报告]
发表于 2012-12-19 11:20 |只看该作者
把两个文件写到一起就没什么问题。
import诡异呀~

论坛徽章:
0
4 [报告]
发表于 2012-12-19 11:31 |只看该作者
IndentationError: expected an indented block
表示是缩进错误。
Python中通过缩进,决定代码之间的逻辑关系。这个你应该知道吧?

所以,再去检查一下,你那出错的代码的位置,是否有多余的Tab,空格等。

关于Tab,空格等,正常肯定是看不到的。
可以使用Notepad++等工具去查看。
相关内容可参考:
Notepad++支持显示回车符,换行符,TAB键,行首,行尾等特殊字符

谢谢楼上,在Ubuntu那边碰到过你,去过你的网页。缩进对python来说是个问题,
才学python经常遇到这类似的问题。看来我要去配置一下我的编辑器了。

论坛徽章:
0
5 [报告]
发表于 2012-12-19 17:48 |只看该作者
另外,对于你此处,在import和下面的def function()之间,写上一个"""xxx""",表示一个多行的字符串
这种写法。
我之前还真没见过。

个人觉得,比较好的做法是:
1. 如果只是针对函数的,那么将多行注释移动到def function()下面变成:
  1. def function():
  2.     """xxx
  3.     xxx"""
  4.     yyyyy
复制代码
2. 如果是针对模块的注释,那么放到import之前:

  1. """xxxx
  2. xxx"""

  3. import xxx;

  4. xxxx
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP