Python打开文件并将名称列表放在单独的行上

Python打开文件并将名称列表放在单独的行上,python,file-io,Python,File Io,我正在尝试使用Python3编写一个python程序 我必须打开一个文本文件,读取姓名列表,打印列表,按字母顺序排序,最后重新打印列表。 还有一点,但我的问题是,我应该打印名单上的名字与每个名字在一个单独的行 它不是将每个名称打印在单独的一行上,而是将列表全部打印在一行上。 我怎样才能解决这个问题 def main(): #create control loop keep_going = 'y' #Open name file

我正在尝试使用Python3编写一个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()
def main():
#创建控制循环
继续前进
#打开名称文件
name_file=open('names.txt','r')
names=name\u file.readlines()
name_file.close()
#开放式出铁口
outfile=open('sorted_names.txt','w')
索引=0
而索引
问题在这里:
打印('原始订单:',名称)
。这是在一行中打印所有列表。因此,请在新行中打印列表中的每个元素,您必须执行以下操作:

print('original order:')
for name in names:
    print(name)
names.sort()
print('sorted order:')
for name in names:
    print(name)

我很抱歉。我使用的是WingIDE1014.1,我把它搞砸了。@julio:
print
是Python3中的一个函数。请考虑编辑你的答案,包括删除“Pythic”。如果print是一个语句被认为是“pythonic”,它就不会被改变。@John,你是对的,我是用Python 2.*来思考的,但是,正如你所知道的,Python 3.*并不是对Python 2.*的修正,只是另一个分支,所以我一直认为
print
作为一个语句更“pythonic”。