我可以使用单独的文件来完成此操作,但如何在文件的开头添加一行?
f=open('log.txt','a')
f.seek(0) #get to the first position
f.write("text")
f.close()
Run Code Online (Sandbox Code Playgroud)
由于文件以追加模式打开,因此从文件末尾开始写入.
我想在我创建的CSV文件中写一些注释pandas.我没有在标准模块中找到任何选项DataFrame.to_csv(即使read_csv可以跳过注释)csv.我可以打开文件,写下注释(以#开头的行#),然后将其传递给to_csv.有没有更好的选择?