通读文件并过滤文本,即只输出符合特定标准的单词

时间:2014-02-27 07:41:18

标签: python-3.3

以下是我的任务依据: 编写一个通过textfile.txt读取的脚本。该脚本将创建一个名为textfile-selected-words.txt的文件。新的textfile-selected-words.txt文件应包含textfile.txt中包含字母'j'的所有单词,大写或小写。如果textfile.txt中的单词不包含字母'j',则会被“忽略”。 我将使用文件对象的readline方法。还建议使用in运算符。

这是我能够提出的,但是新的textfile-selected-words.txt文件正在创建但不包含任何内容。我不知道我错过了哪一步。

f = open('textfile.txt')
h = open('textfile-selected-words.txt', 'w')
line = f.readline()
while line:
    if 'j' in line and 'J' in line:
        h.write()
        line = f.readline()
f.close()
h.close()   

0 个答案:

没有答案