[英]find character at last of line in python file move it to start of line
I have to read one python file and whenever i get } closing curly braces at the end of line move it to start of line for eg.我必须阅读一个 python 文件,每当我得到 } 在行尾关闭花括号时,将其移动到行首,例如。 input.py
输入.py
print("hello")
print("shankar")}
required output需要 output
print("hello")
}
print("shankar")
current output getting now当前 output 正在获取
print("hello")
}print("shankar")}
code代码
pattern='}'
f = open('input.py', 'r')
g = open('tempo.py', 'w')
l=f.readlines()
f.close()
for ind in range(len(l)):
if pattern in l[ind]:
l[ind]= "}" + l[ind]
l[ind] = l[ind].rstrip("}")
for field in l:
g.write(field)
g.close()
print('Done')
You may need to consider the newline character:您可能需要考虑换行符:
pattern='}'
f = open('input.py', 'r')
g = open('tempo.py', 'w')
l=f.readlines()
f.close()
for ind in range(len(l)):
if pattern in l[ind]:
l[ind]= "}" + l[ind]
l[ind] = l[ind].replace("}\n","\n")
for field in l:
g.write(field)
g.close()
print('Done')
note: if your line break is windows-like then you may need to do replace ("}\r\n","\r\n")
注意:如果您的换行符类似于 Windows,那么您可能需要
replace ("}\r\n","\r\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.