python方法完成删除并逐行输出效果如何实现,python逐行,刚入门Pyhton, 有
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)
编橙之家文章,
相关内容
- Flask被阻塞了,请问该如何解决呢,flask阻塞解决,gunico
- Django1.7报错问题求助,django1.7问题求助,Django1.7.5
- 关于Jinjia2中wtforms生成html标签时含有'-'属性问题,jin
- python不打开文件情况下如何获得大文件md5值的方法,
- Python mysqldb执行execute报InterfaceError: (0, '')求帮忙解答,
- python中子类父类继承具体指什么?,python中子类继承
- Python什么方法适合处理数据入库乱码问题,python乱码
- Python最适合在Mac使用的图形库是哪个,pythonmac图形库
- Python多线程获取网页数据速度过慢如何调整,python多线
- web.py增加、删除、修改、查询怎么做,web.py查询,其实我
评论关闭