[英]What are the commands to read a .csv file and skip the first row in Python 2.4.3
[英]What is the alternative for open in python 2.4.3
当我在python 2.4.3中使用open()
时,出现以下错误
File "/tmp/chgjdbcconfig.py", line 16 with open("text.new", "wt") as fout: ^ SyntaxError: invalid syntax
我检查了python版本,这是我的输出
Python 2.4.3
我一直在寻找替代方案的建议。 我正在尝试在linux服务器上的XML文件中编辑行,但无法控制python版本的升级。
任何建议都很好!
2.4中不缺少open
。 缺少的是with
语句(在Python 2.5中作为PEP-0343添加)
做同样的代码,而无需with
:
fout = open("text.new", "wt")
# code working with fout file
fout.close()
请注意,在将fout.close()
with
使用时是隐式的,但您需要自己添加它而无需
with
语句具有即使在引发异常时也可以关闭文件的附加好处,因此实际上更精确的模拟是:
fout = open("text.new", "wt")
try:
# code working with fout file
finally:
fout.close()
它不是open()
令人窒息。 它是with
语法。 上下文管理器在Python 2.4中不存在。
您还必须显式关闭文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.