我对Python或任何一般编程语言都是新手。
供参考:
1。)我班的作业是这样的:从文本文件'word.txt'中读取,并将符合条件的第一个文本文件中的单词写入新的文本文件中。 例如,如果函数是getListEnd('s','word.txt','word1.txt'),并且条件是公义中的单词必须全部以字符's'结尾,则我的ifile word.txt = [苹果,胡萝卜,奶酪,芥末,问题]将被分类为普通单词。1.txt= [苹果,胡萝卜,问题]
2。)这是我的代码:
mf1 = open("/Users/XXXX/word.txt", "r")
mf2 = open("/Users/XXXX/word2.txt", "w+")
mylist1=[] #list for storing word.txt strings
mylist2=[] #list for storing sorted strings
def sort(c): #function for sorting mylist1 for
for i in mylist1: criteria into my list2
if c==i[-1]:
mylist2.append(i)
def getListEnd(c, ifile, ofile):
for s in mf1: #loop for appending word.txt to list 1
mylist1.append(s)
print(len(mylist1)) #check if mylist1 contains word.txt
sort(c) #append sorted words to list2
with open('word2.txt', 'w+') as mf2: #write into word1.txt list2
for listitem in mylist2:
mf2.write('%s\n' % listitem)
getListEnd('s', 'word.txt', 'word2.txt')
3。)因此,问题是:为什么当我尝试将已排序的单词附加到mylist2时什么也没发生?也就是说,当我检查mylist2的长度时,即使在word.txt文件中有很多以s结尾的单词,它也显示为0?我知道在检查myList1中是否存在以最后一个字母's'结尾的字符时出错,但是我不知道该怎么办,也无法将其追加到mylist2中。
答案 0 :(得分:0)
程序有两点错误:
这是您的程序,其中包含有关更改的注释:
# mf1 = open("word.txt", "r") # Don't do this here. You read it in in your with block anyway.
# mf2 = open("word2.txt", "w+"). # Don't do this here. You write it in your with block anyway
mylist1=[]
mylist2=[]
def sort(c):
for i in mylist1:
if c==i[-1]:
mylist2.append(i)
def getListEnd(c, ifile):
with open(ifile, "r") as mf1:
for s in mf1: # the strings will still contain the newlines if read this way
# so when you check the end of the string you'll actually be comparing
# a newline character.
if "\n" in s:
mylist1.append(s[:-1]) # if there's a newline character,
# don't include it when appending to the list
else:
mylist1.append(s)
print("mylist length: ", len(mylist1))
print("mylist1: ", mylist1)
sort(c)
getListEnd('s', 'word.txt') # you don't need to pass it any info about the output file
print("mylist2: ", mylist2)
## Your writing block has to come after you've sorted it! Not before
with open('word2.txt', 'w+') as mf2:
for listitem in mylist2:
mf2.write('%s\n' % listitem)
要创建仅包含字母“ s”结尾的项目的列表,只需执行以下操作:
input_list = ['apples', 'carrot', 'johns', 'list']
output = [i for i in input_list if "s" == i[-1]]
print(output)
>> ['apples', 'johns']
第output = [i for i in input_list if "s" == i[-1]
行使用input_list
遍历i
列表,并检查每一项,以查看每个字符串i[-1]
的最后一个字符是否为字符{{1 }}。然后,它返回一个列表,其中包含与条件匹配的所有项目。一种更具表现力的编码方式是:
's'
通过读写文件,使用python在for word in input_list:
if word[-1] == "s":
output.append(word)
运算符中进行处理是更安全的,因为它可以确保您没有未关闭的文件挂在周围,或者更安全。
这是一个玩具程序,我认为它可以满足您的要求:
with
如果word.txt是:
with open("word.txt", "r") as f:
mylist1 = f.read().split("\n") # splits the input file around newlines
mylist2 = [i for i in mylist1 if "s" == i[-1]]
with open("word2.txt", "w") as f:
for word in mylist2:
f.write(f"{word}\n") # writes word into the file
然后word2.txt变为:
john
apples
carrots
lewis
答案 1 :(得分:0)
通过for x in file
循环从文件中读取行,可以在每行末尾提供换行符。
因此,i[-1]
是换行符,而不是单词的最后一个字母。
答案 2 :(得分:0)
这里是一个例子:
with open('words1.txt', 'w') as f_object:
f_object.write("Apples" + "\n")
f_object.write("Rubles" + "\n")
f_object.write("Nope" + "\n")
f_object.write("Nah" + "\n")
f_object.write("Georges" + "\n")
f_object.close()
print("Done")
with open('words1.txt', 'r') as f:
lines = f.readlines()
with open('words2.txt', 'w') as f_ob:
for line in lines:
line = line.rstrip()
if line[-1] == 's':
f_ob.write(line + '\n')
print("Done")
我们可以简单地做出一个条件,如果单词以's'结尾,我们将其添加到新的.txt文件中。例如,当我们遍历每一行时,我们可以获取字符串中的最后一个索引。 “如果”该行的最后一个索引等于s,那么我们显然可以将该行写入文件。如果没有,它将跳过。您的word2.txt文件中的输出将为:
Apples
Rubles
Georges