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

初学者询问如何使用Python实现用户输入关键词后,从文本中删除含有该关键词的行,并按行输出处理后的文本。已给出部分代码,但未能实现逐行输出。寻求改进方案。
摘要由CSDN通过智能技术生成

刚入门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)

运行结果:

54a9751677bd73af82bb1765b268c688.png

这样显示太乱了?求教!

#!/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)

828d9832346dadcf9449413fd12c4cdc.png

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

相关文章 Recommend

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值