- 论坛徽章:
- 0
|
talk is cheap, see the code
nester.py:- import sys
- """一个简单的用于输出嵌套列表的函数
- the_list: 输出的列表
- level: 每层缩进的TAB数量
- stream: 输出的文件默认是标准输出"""
- def print_lol(the_list, indent, level=0, stream=sys.stdout):
- for item in the_list:
- if isinstance(item, list):
- print_lol(item, indent, level+1, stream)
- else:
- if indent:
- for num in range(level):
- print("\t", file=stream, end='')
- print(item, file=stream)
复制代码 sketch.py:- import nester
- man = []
- other = []
- try :
- data = open('sketch.txt')
- for each_line in data :
- try :
- (role, line_spoken) = each_line.split(":", 1)
- line_spoken = line_spoken.strip()
- if role == 'Man' :
- man.append(line_spoken)
- elif role == 'Other Man' :
- other.append(line_spoken)
- except ValueError :
- pass
- data.close()
- except IOError:
- print('The datafile is missing!')
- try :
- with open("man.out", "w") as out_man, open("other_man.out", "w") as out_other :
- print_lol(man, False, 0, out_man)
- print_lol(other, False, 0, out_other)
- except IOError as err :
- print("file error: " + str(err))
复制代码 问题出在sketch.py中的import nester那行。
我在idle编辑器中F5运行nester.py,一切正常,在python shell中输入import nester正常,
并且可以调用nester模块中的函数print_lol。
但是,当我运行完nester.py紧接着运行sketch.py的时候会出现错误。如下:- Traceback (most recent call last):
- File "/home/ptdzm/code/python_lib/pytest/chapter3/sketch.py", line 1, in <module>
- import nester
- File "/home/ptdzm/code/python_lib/nester.py", line 6
- """the_list: 是要显示的列表可以是嵌套列表
- level: 是需要缩进的层数,每当遇到嵌套列表
- 的时候就缩进level个制表符"""
-
-
- ^
- IndentationError: expected an indented block
复制代码 请问各位这是什么问题?感谢任何形式的帮助~ |
|