Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重新排序文本文件-Python_Python_File_Text_Input_Output - Fatal编程技术网

重新排序文本文件-Python

重新排序文本文件-Python,python,file,text,input,output,Python,File,Text,Input,Output,我必须重新排序输入文件,然后将输出打印到新文件 这是输入文件: The first line never changes. The second line was a bit much longer. The third line was short. The fourth line was nearly the longer line. The fifth was tiny. The sixth line is just one line more.

我必须重新排序输入文件,然后将输出打印到新文件

这是输入文件:

 The first line never changes.  
 The second line was a bit much longer.  
 The third line was short.  
 The fourth line was nearly the longer line.    
 The fifth was tiny.  
 The sixth line is just one line more.                  
 The seventh line was the last line of the original file. 
这是输出文件的外观:

 The first line never changes.                                            
 The seventh line was the last line of the original file.
 The second line was a bit much longer. 
 The sixth line is just one line more.
 The third line was short. 
 The fifth was tiny. 
 The fourth line was nearly the longer line.
我已经有代码可以反转输入文件并将其打印到输出文件,如下所示

ifile_name = open(ifile_name, 'r')
lines = ifile_name.readlines()
ofile_name = open(ofile_name, "w")

lines[-1] = lines[-1].rstrip() + '\n'
for line in reversed(lines):
        ofile_name.write(line)
ifile_name.close()
ofile_name.close()
在保留反向代码的同时,是否可以在文本文件中获得所需的格式

例如打印输入文件的第一行,然后反转并打印该行,打印输入文件的第二行,然后反转并打印该行等

抱歉,如果这看起来不清楚,我对Python和堆栈溢出非常陌生

提前感谢。

ifile\u name=“hello/input.txt”
ifile_name = "hello/input.txt"
ofile_name = "hello/output.txt"
ifile_name = open(ifile_name, 'r')
lines = ifile_name.readlines()
ofile_name = open(ofile_name, "w")

lines[-1] = lines[-1].rstrip() + '\n'
start = 0
end = len(lines) - 1
while start < end:
    ofile_name.write(lines[start])
    ofile_name.write(lines[end])
    start += 1
    end -= 1
if start == end:
    ofile_name.write(lines[start])
ifile_name.close()
ofile_name.close()
ofile_name=“hello/output.txt” ifile\u name=open(ifile\u name,'r') lines=ifile\u name.readlines() 文件名=打开(文件名,“w”) 行[-1]=行[-1].rstrip()+'\n' 开始=0 结束=长度(线)-1 开始<结束时: ofile_name.write(行[开始]) 文件名。写入(行[结束]) 开始+=1 结束-=1 如果开始=结束: ofile_name.write(行[开始]) ifile_name.close() ofile_name.close()
使用两个枢轴
start
end
指向要写入文件的行。
一旦
开始
=
结束
,将中间一行写入文件

如果您不关心生成的列表,我相信这是一个非常优雅的解决方案

with open("ifile_name","r") as f:
    init_list=f.read().strip().splitlines()

with open("result.txt","a") as f1:
    while True:
        try:
            f1.write(init_list.pop(0)+"\n")
            f1.write(init_list.pop()+"\n")
        except IndexError:
            break