python即时标记看不懂怎么办_Python-学习-项目1-即时标记-1

买了一本Python入门,奈何看不下去,只能是先看后面的项目,看到那里不懂的时候在回去学习。

项目名字:即时标记

大致的意思就是把一个纯文本文件标记成自己想要的格式文件。

首先就是待处理文本,我找不到电子版的,就自己手写了一份

Welcome to World Wide Spam, Inc

These are the corporate web pages of *World Wide Spam*, Inc. We hopeyou find your enjoyable, and that you will sample many of our products

A short history of the company

World Wide Spam was started in the summer of 2000. The business concept was to ride the dot-com wave and to make money both through bulk email and by selling canned meat online

After receiving several complaints from customer who weren't satisfied bu their bulk email .World Wide Spam altered their profile. and foused 100% on canned goods. Today they rank as the world's 13.892nd online suppler of SPAM

Destinations

From this page you may visit several of our interesting web pages:

-What is SPAM?(WWW.baidu.com)

-How do they make it?(WWW.baidu.com)

-Why should i eat is?(WWWW.baidu.com)

How to get in touch with us

You can get in touch with us in *many* ways: By phone(123456789). by email(dream_dog@163.com) or by visiting our customer feedback page(wwww.baidu.com)

第一步,就是那文件切分成段落。

找出块的一个简单方法就是搜集遇到的所有行,知道遇到一个空行,然后返回已经搜集的行。那些返回的行就是一个块,之后在开始收集,不需要手机空行,也不要返回空块,同时要确保文件的最后一行是空行,否则程序就不知道什么时候结束

编写一个文件快生成器

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 = []

代码中,lines生成器只是在文件尾追加一行空行,blocks生成器实现了前面说的方法。

添加一些标记

import sys. re

from util import *

print('

...')

title = True

for block in blocks(sys.stdin):

block = re.sub(r'\*(.+?)\*',r'\1',block)

if title:

print('

')

print(block)

print('')

title = false

else:

print('

')

print(block)

print('

')

print('')

这里抱错了,显示我的sys不是一个包

2797188d53399196c49f92fe8f126f78.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值