[Python] Write to csv file with Python

Introduction

Python code for writing csv files. Specify the directory and set the file name to date, time, etc. By adding date and time information to the file name, a file with a unique name can be created when it is executed, which is convenient for saving logs.

environment

code

Output csv to the ʻoutput` directory of the same directory as csv_log.py.

csv_log.py


import datetime
import csv

#Get date and time
now = datetime.datetime.now()
#Specify the directory here
filename = './output/log_' + now.strftime('%Y%m%d_%H%M%S') + '.csv'

#File, first line(column)Creation
with open(filename, 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['x','y','z'])

x,y,z = 0,0,0
while(1):
    #Write some processing
    x += 1
    y += 2
    z += 3
    #Match filename to the name of the file you created
    # writer.write list to csv with writerow
    with open(filename, 'a', newline="") as f:
        writer = csv.writer(f)
        writer.writerow([x, y, z])

    #Break when processing is finished
    break

I think you can make a csv like this

x y z
1 2 3
4 5 6
: : :

in conclusion

That's all for this time, but I often use pandas for data analysis, etc. I can easily output csv with pandas, so I will update it again. Thank you very much!

Recommended Posts

[Python] Write to csv file with Python
Write to csv with Python
Output to csv file with Python
Download csv file with python
Write CSV data to AWS-S3 with AWS-Lambda + Python
I want to write to a file with Python
I tried to touch the CSV file with Python
How to convert JSON file to CSV file with Python Pandas
[Python] A memo to write CSV vertically with Pandas
[Python] How to convert db file to csv
Read CSV file with python (Download & parse CSV file)
[Python] Convert csv file delimiters to tab delimiters
[Part1] Scraping with Python → Organize to csv!
Csv tinkering with python
Read Python csv file
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
Scraping tabelog with python and outputting to CSV
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Writing logs to CSV file (Python, C language)
[Write to map with plotly] Dynamic visualization with plotly [python]
Read CSV file with Python and convert it to DataFrame as it is
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
Read and write csv file
Connect to BigQuery with Python
Read csv with python pandas
Draw netCDF file with python
Connect to Wikipedia with Python
Post to slack with Python 3
Let's write python with cinema4d.
Switch python to 2.7 with alternatives
How to measure mp3 file playback time with python
Write multiple records to DynamoDB with Lambda (Python, JavaScript)
A program to write Lattice Hinge with Rhinoceros with Python
For those who want to write Python with vim
[Python-pptx] Output PowerPoint font information to csv with python
Python script to create a JSON file from a CSV file
How to write a Python class
Link to get started with python
Make apache log csv with python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
[Python] Read the csv file and display the figure with matplotlib
Write standard output to a file
Create an Excel file with Python3
Convert list to DataFrame with python
Output cell to file with Colaboratory
MP3 to WAV conversion with Python
To do tail recursion with Python2
How to get started with Python
Write FW to TWE-Lite with TWE-Lite R.
Python CSV file reading and writing
What to do with PYTHON release?
Unable to install Python with pyenv
Handle Excel CSV files with Python
How to use FTP with Python
How to calculate date with python
Reading and writing CSV with Python