![](/img/trans.png)
[英]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.