我想写使用Python 3Python中打开文件,并在单独的行把名单
我不得不打开一个文本文件,并宣读的名单,打印列表python程序,排序按字母顺序列表,然后重新打印列表。 还有一点比它强,但我遇到的问题是,我应该在单独的行上打印每个名称的名称列表
而不是在单独的行上打印每个名称,它会打印该列表全部在一行上。 我该如何解决这个问题?
def main():
#create control loop
keep_going = 'y'
#Open name file
name_file = open('names.txt', 'r')
names = name_file.readlines()
name_file.close()
#Open outfile
outfile = open('sorted_names.txt', 'w')
index = 0
while index < len(names):
names[index] = names[index].rstrip('\n')
index += 1
#sort names
print('original order:', names)
names.sort()
print('sorted order:', names)
#write names to outfile
for item in names:
outfile.write(item + '\n')
#close outfile
outfile.close()
#search names
while keep_going == 'y' or keep_going == 'Y':
search = input('Enter a name to search: ')
if search in names:
print(search, 'was found in the list.')
keep_going = input('Would you like to do another search Y for yes: ')
else:
print(search, 'was not found.')
keep_going = input('Would you like to do another search Y for yes: ')
main()
怪蛇4 ??????? – juliomalegria
我不好意思。我正在使用Wing IDE 101 4.1,我把它搞砸了。 –