免费注册 查看新帖 |

Chinaunix

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

看不懂报错内容是啥意思(python基础教程第二版示例1) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-19 13:50 |只看该作者 |倒序浏览
python基础教程第二版中第20章有个即时标记的例子,先把普通的文本文件用生成器分块再用HTML标签自动注释的例子,我按照书中的例子编译总是报错
   报错内容:D:\code\2mark>simple_markup.py <test_input.txt>  test_output.html
             Traceback (most recent call last):
                         File "D:\code\2mark\simple_markup.py",line 2,in <module>
                             from util import *
                         File "D:\code\2mark\util.py",line 3
                             yield '\n'

                     IndentationError: unexpected indent

util.py的代码是:

def lines(file):
   for line in file:yield line
         yield '\n'

def blocks(file):
    block=[]
    for line in lines(file):
        if line.strip():
           block.append(line)
        elif block:
           yield ''.join(block).strip()
           block=[]

simple_markup.py的代码是:

import sys,re
from util import *
print '<html><head><title>...</title><body>'
title = True
for block in blocks(sys.stdin):
    block=re.sub(r'\*(.+?)\*',r'<em>\1</em>',block)
    if title:
         print "<h1>'
         print block
         print '</h1>'
         title=False
    else:
        print '<p>'
        print block
        print '</p>'
        print '</body></html>'

修改了多次不知道哪出问题了编译不过去,请教~

论坛徽章:
0
2 [报告]
发表于 2011-10-19 15:02 |只看该作者
不是提示你了啊
IndentationError: unexpected indent
IndentationError:意外的缩进
用个文本编辑器把空格和tab都显示出来你就能看到你的代码哪里缩进不对了,空格和tab看起来一样宽也是属于缩进异常的

论坛徽章:
0
3 [报告]
发表于 2011-10-19 17:19 |只看该作者
楼上正解,python初学者的常见问题,python是靠缩进区分代码块的,切记。

论坛徽章:
0
4 [报告]
发表于 2011-10-20 09:40 |只看该作者
感激楼上两位,改正中~~~~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP