镜像文件中的文本并将其写入另一个文件

时间:2017-10-22 11:59:42

标签: python python-2.7 python-3.x

如何从文件中读取并将每个参数写入不同的文件。但每条线都必须颠倒和居中。 clarity == VS1部分

似乎有些问题
#write mirror file

运行此代码时出现的错误是

#some variables
Lines = 0
ShortestLine = 80
Longestline = 0
Emptylines = 0
width = 80
Fillchar = " "

#open files
file = "radishsurvey.txt"
p = open (file,"r")
q = open ("mirroredfile.txt","w")

#loop to count lines and length
for line in p:
    Lines +=1
    length = len(line)
    if length < ShortestLine:
        ShortestLine = length
    if length > Longestline:
        Longestline = length
    if length == 0:
        Emptylines += 1

#write mirror file
    q.write("{0:}\n".format(p.reversedString(line[-2::-1]).center(80)))

q.close()
p.close()

1 个答案:

答案 0 :(得分:0)

“W”先清理你的文件,然后写下你的新数据!你应该附加你的文件,用“a”来保存以前的数据......