玩蛇网提供最新Python编程技术信息以及Python资源下载!

python方法完成删除并逐行输出效果如何实现

刚入门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 os
os.chdir('/Users/apple/Desktop/Python/chapter')

some_word = raw_input('Input your word: ').strip()  # strip is to delete useless space
count = 0
keep = []

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 python3
import os

some_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)

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19585.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月17日 - 10时59分01秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules