[PYTHON] Read and write csv file

import csv

with open('test.csv', 'w+', newline='') as csv_file:
    fieldnames = ['Name', 'Count']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'Name':'A', 'Count':1})
    writer.writerow({'Name':'B', 'Count':2})
    
    csv_file.seek(0)
    
    reader = csv.DictReader(csv_file)
    
    for row in reader:
        print(row['Name'], row['Count'])

Execution result


A 1
B 2

Recommended Posts

Read and write csv file
Read and write a file
Write and read a file
Read CSV file: pandas
Read and write csv files with numpy
Read Python csv file
Read the csv file and display it in the browser
Read file
[Python] Write to csv file with Python
Python CSV file reading and writing
[Python] Read the csv file and display the figure with matplotlib
Read the csv file with jupyter notebook and write the graph on top of it
Read zipline brand data from csv file and perform back test
Read CSV file with python (Download & parse CSV file)
Bind to class to read and write YAML
Write O_SYNC file in C and Python
Read Python csv and export to txt
Read and write JSON files in Python
[pandas] .csv file reading and display method
[python] Read html file and practice scraping
Read CSV and analyze with Pandas and Seaborn
Read and format a csv file mixed with comma tabs with Python pandas
Read CSV file with Python and convert it to DataFrame as it is
How to read a CSV file with Python 2/3
Read JSON with Python and output as CSV
[Python3] Read and write with datetime isoformat with json
[Introduction to Pandas] Read a csv file without a column name and give it a column name
Fourier transform the wav file read by Python, reverse transform it, and write it again.
File read overhead measurement
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
Read and write single precision floating point in Python
Read and write files with Slackbot ~ Bot development with Python ~
Read json file with Python, format it, and output json
Read and write NFC tags in python using PaSoRi
Compare read / write speed and capacity of csv, pickle, joblib, parquet in python environment
Read csv with python pandas
Device and Linux file system
Reading and writing csv files
Read / write between BigQuery ↔ Pandas
Linux file and directory permissions
Write to csv with Python
Read the VTK file and display the color map with jupyter.
AHC task (1) Reading CSV file
[Python] Use this to read and write wav files [wavio]
Download csv file with python
Extract csv data and calculate
[Python] Read a csv file with a large data size using a generator
Read, change, and write Excel files on GCP App Engine Storage
Format the Git log and get the committed file name in csv format
Create and return a CP932 CSV file for Excel with Chalice
Grayscale image and save as csv
Run Pylint and read the results
Output to csv file with Python
Switch argparse file specification and pipeline
Write standard output to a file
Read and use Python files from Python
Preserve and read order in PyYAML
[Linux] File and directory operation commands
Python memo ① Folder and file operations
Reading and writing CSV with Python
Ignore # line and read in pandas