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