Closing file after using to_csv()

后端 未结 3 805
猫巷女王i
猫巷女王i 2020-12-19 19:37

I am new to python and so far I am loving the ipython notebook for learning. Am I using the to_csv() function to write out a pandas dataframe out to a file. I wanted to o

3条回答
  •  囚心锁ツ
    2020-12-19 20:27

    This is the better way of doing it. With context manager, you don't have to handle the file resource.

    with open("thefile.csv", "w") as f:
        df.to_csv(f)
    

提交回复
热议问题