[Python] How to make the file name of the output data unique (use year, month, day, hour, second)

[Python] How to make the file name of the output data unique (use year, month, day, hour, second)

When I specify the file name with the to_csv method of the pandas module and output it, it is overwritten and the old file disappears.

A method to output individually and prevent overwriting by including the date and time of execution in the file name.


## manner Use the following modules and methods

① datetime module ② now method ③ strftime method ④ format function


## Illustration

code


import datetime as dt
now = dt.datetime.now()

time = now.strftime('%Y%m%d-%H%M%S')

df.to_csv('~/desktop/output_{}.csv'.format(time), index=False, encoding='utf_8_sig')
image.png

A separate file is generated each time you run it.


## Contents of each code ## 1. Find the current time with the now method of datetime.

datetime now method



import datetime as dt
now = dt.datetime.now()

now

#output
# datetime.datetime(2020, 3, 27, 19, 16, 41, 332644)

About datetime [here](# https://qiita.com/yuta-38/items/eb2ad47e8a25bde21c6c)


## 2. Specify the date format with the strftime method of datetime.

datetime strftime method


time = now.strftime('%Y%m%d-%H%M%S')
time

#output
#'20200327-191641'

-Detailed explanation of strftime method is here

・ For the types of specifiers (% Y,% d, etc.), click here (https://qiita.com/yuta-38/items/ba6dce967ede22e37c60#%E6%97%A5%E4%BB%98%E3% 81% AE% E6% 8C% 87% E5% AE% 9A% E5% AD% 90% E4% B8% 80% E8% A6% A7)


## 3. Make variables available in filenames with the format function.

format function


time = now.strftime('%Y%m%d-%H%M%S')

'output_{}.csv'.format(time)

#output
# 'output_20200327-193206.csv'

Click here for details of the format function [https://qiita.com/yuta-38/items/9a93eea10ccc7ac2c8ad)


## 4. Output as a csv file with the to_csv method

csv file output


df.to_csv('~/desktop/output_{}.csv'.format(time), index=False, encoding='utf_8_sig')

For the to_csv method, click here (https://qiita.com/yuta-38/items/4ee7518b1e82e60822f8)

Recommended Posts

[Python] How to make the file name of the output data unique (use year, month, day, hour, second)
Note: How to get the last day of the month with python (added the first day of the month)
[Python] How to get the first and last days of the month
I want to output the beginning of the next month with Python
How to output the output result of the Linux man command to a file
[Python] Summary of how to use pandas
How to use "deque" for Python data
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
[Question] How to use plot_surface of python
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
python I don't know how to get the printer name that I usually use.
[Python] How to use two types of type ()
Study from Python Hour7: How to use classes
[Python] How to name table data and output it in csv (to_csv method)
Summary of how to use MNIST in Python
[Algorithm x Python] How to use the list
How to erase the characters output by Python
[Introduction to Python] How to get the index of data with a for statement
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
Output the specified table of Oracle database in Python to Excel for each file
How to get the variable name itself in python
Template of python script to read the contents of the file
How to get the number of digits in Python
I tried to summarize how to use matplotlib of python
How to use the Raspberry Pi relay module Python
How to use Python Kivy ① ~ Basics of Kv Language ~
Let's use the open data of "Mamebus" in Python
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] Summary of how to specify the color of the figure
[Python] How to output the list values in order
[Deep learning] Investigating how to use each function of the convolutional neural network [DW day 3]
[Python] How to make a list of character strings character by character
[python] Change the image file name to a serial number
[python] How to use the library Matplotlib for drawing graphs
Make the display of Python module exceptions easier to understand
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
[Hyperledger Iroha] Notes on how to use the Python SDK
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
I didn't know how to use the [python] for statement
How to see the contents of the Jupyter notebook ipynb file
[Python] How to output a pandas table to an excel file
[Introduction to Python] How to get data with the listdir function
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
python3: How to use bottle
How to use Python bytes