我想寫使用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,我把它搞砸了。 –