python方法完成删除并逐行输出效果如何实现,python逐行,刚入门Pyhton, 有


刚入门Pyhton, 有些问题不是很懂,特此来请教。

一个我想要实现的功能:
让用户输入一串字符,然后删除文章有这些字符串的文字,然后按行输出一个新的文本。


具体功能要求如下

原来的文本:
Jobs said: The device I'm about to introduce to you is gonna revolutionize an entire industry.
Jobs said: It's a music playing device. Okay. We'll get to that in a minute.
Jobs said: Because what it represents is as important as what it is. It's a tool for the heart.
Jobs said: And when you can touch someone's heart, that's limitless. If I do say so myself, it's insanely cool.
Jobs said: It's a music player. It's a thousand songs in your pocket.
Jobs said: I’d like to introduce you to the iPod.

处理:

enter your words: Jobs said:

处理之后的文本:
The device I'm about to introduce to you is gonna revolutionize an entire industry.
It's a music playing device. Okay. We'll get to that in a minute.
Because what it represents is as important as what it is. It's a tool for the heart.
And when you can touch someone's heart, that's limitless. If I do say so myself, it's insanely cool.
It's a music player. It's a thousand songs in your pocket.
I'd like to introduce you to the iPod.


我已经完成的部分,如何改进我的代码?(功能未完成:主要是按行输出未能实现)

import osos.chdir('/Users/apple/Desktop/Python/chapter')some_word = raw_input('Input your word: ').strip()  # strip is to delete useless spacecount = 0keep = []try:    data = open('Jobs.txt')    for line in data:        for word in line.split():            if word not in some_word:                keep.append(word)except IOError:    print('The datafile is missing')data.close()print(keep)

运行结果:

这样显示太乱了?求教!

#!/usr/bin/env python3import ossome_word = input('Input your word: ').strip()keep = ""for line in open('Jobs.txt'):    keep += ''.join(str(i) for i in line.split(some_word))print(keep)

编橙之家文章,

评论关闭