Write to csv with Python

Purpose: Used when you want to write something in a text file, etc.

# 'w'about
('test.csv', 'w', encoding='utf-8')
Notation Overview
r Read
w Write new
a Write in postscript
r+ An error will occur if the additional write / read file does not exist.
w+ Create if there are no new write and read files
a+ Write and read additional notes, create if there is no file

Write with Hello World


with open('test.csv', 'w', encoding='utf-8') as f:
    f.write('Hello World\n')

print('I wrote it.')

I wrote it.


Hellor World

Processing when outputting multiple lines


lang = ['SQL',

with open('lang.csv', 'w', encoding = 'utf-8') as f:
    for item in lang:
        f.write(item + '\n')
print('I wrote it.')

Execution result



Applied date manipulation


from datetime import datetime

now = datetime.now()
#print(now) 2020-10-27 19:56:15.019242

str_now = f'{now:%Y/%m/%d %H:%M}'
#print(str_now) 2020/10/27 19:56

with open('time.csv', 'w', encoding='utf-8') as f:
    f.write(str_now + ' -holiday\n')

print('I wrote it.')

Execution result


2020/10/27 20:00 -holiday

Recommended Posts

Write to csv with Python
[Python] Write to csv file with Python
Write CSV data to AWS-S3 with AWS-Lambda + Python
Output to csv file with Python
[Python] A memo to write CSV vertically with Pandas
[Part1] Scraping with Python → Organize to csv!
Csv tinkering with python
How to read a CSV file with Python 2/3
[Write to map with plotly] Dynamic visualization with plotly [python]
I want to write to a file with Python
Connect to BigQuery with Python
Read csv with python pandas
Connect to Wikipedia with Python
Post to slack with Python 3
Let's write python with cinema4d.
Download csv file with python
I tried to touch the CSV file with Python
How to convert JSON file to CSV file with Python Pandas
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
How to write a Python class
Python: How to use async with
Link to get started with python
Make apache log csv with python
Create folders from '01' to '12' with python
Try to operate Facebook with Python
Convert list to DataFrame with python
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.
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
Easily post to twitter with Python 3
I want to debug with Python
Write a batch script with Python3.5 ~
How to write offline real-time Solving E05 problems with Python
Write documentation in Sphinx with Python Livereload
[Python] How to convert db file to csv
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
FizzBuzz with Python3
Updated to Python 2.7.9
Try to reproduce color film with Python
Scraping with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
Don't write Python if you want to speed it up with Python
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
Read CSV file with python (Download & parse CSV file)
Scraping with Python
Output color characters to pretty with python
Python with Go