[PYTHON] Reading and writing csv files

csv file writing

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 reading

with open ('test.csv', 'r', newline='') as csv_file:
    reader = csv.DictReader(csv_file)
    for row in reader:
        print(row['Name'], row['Count'])

Recommended Posts

Reading and writing csv files
Reading and writing CSV and JSON files in Python
Python CSV file reading and writing
Reading and writing CSV with Python
[Introduction for beginners] Reading and writing Python CSV files
Reading and writing JSON files with Python
[Python] Reading CSV files
Study from Python Reading and writing Hour9 files
Reading and writing fits files with Python (memo)
Case sensitive when reading and writing INI files
Example of reading and writing CSV with Python
Character code for reading and writing csv files with python ~ windows environment ver ~
Reading and writing NetCDF with Python
Reading and writing text in Python
uproot: Python / Numpy based library for reading and writing ROOT files
Read and write csv files with numpy
[pandas] .csv file reading and display method
Linux: files and directories
Read and write csv file
Linux (about files and directories)
About LINUX files and processes
Memorandum (Countermeasures against Unicode Decode Error when reading CSV files)
Reading .txt files with Python
AHC task (1) Reading CSV file
Notes on reading and writing float32 TIFF images in python
Extract csv data and calculate
Reading and writing Files from notebook on Watson Studio to IBM Cloud Object Storage (ICOS)-using project-lib-
How to import CSV and TSV files into SQLite with Python
Transpose CSV files in Python Part 1
[Python] Loading csv files using pandas
Grayscale image and save as csv
Handle csv files with Django (django-import-export)
Scraping, preprocessing and writing to postgreSQL
Manipulate files and folders in Python
Read and use Python files from Python
Save and retrieve files with Pepper
Handle Excel CSV files with Python
Export and output files in Python
[Python] Reading and writing photo location information tags (JPG file GPS Exif)
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
Wrap reading and writing of GCP to Secret Manager with google subcommands
[Python] Master the reading of csv files. List of main options for pandas.read_csv.