Reading and writing CSV with Python

A memo for reading and writing CSV files using Python. [** 3. Pandas method **](http://qiita.com/okadate/items/c36f4eb9506b358fb608#3-pandas%E3%82%92%E4%BD%BF%E3%81%86%E5% A0% B4% E5% 90% 88% E3% 81% 8A% E3% 81% 99% E3% 81% 99% E3% 82% 81) is recommended.

2014/07/28 Addition of reading (Pandas).
2014/11/28 Summarize the case of using pandas.

** 1. Read **

Use the with statement by referring to Python documentation.

import csv

with open('some.csv', 'r') as f:
    reader = csv.reader(f)
    header = next(reader)  #When you want to skip the header

    for row in reader:
        print row          #Can be obtained line by line

It can be read as follows without using the with statement.

import csv

f = open('some.csv', 'r')

reader = csv.reader(f)
header = next(reader)
for row in reader:
    print row

f.close()

In this case, add a close statement.

** 2. Write **

The with statement is also used for writing.

import csv

with open('some.csv', 'w') as f:
    writer = csv.writer(f, lineterminator='\n') #Line feed code (\n) is specified
    writer.writerow(list)     #For list (one-dimensional array)
    writer.writerows(array2d) #You can also write a two-dimensional array

As with reading, it is OK without with.

import csv

f = open('some.csv', 'w')

writer = csv.writer(f, lineterminator='\n')
writer.writerow(list)
writer.writerows(array2d)

f.close()

** 3. How to use pandas ** (Recommended)

Reading with pandas is neat and often convenient.

import pandas as pd

df = pd.read_csv('some.csv')

print df       # show all column
print df['A']  # show 'A' column

The read DataFrame is easy to write.

df.to_csv('some2.csv')

For how to use it, refer to ** Summary of grammar often used in pandas ** @okadate --Qiita.

Recommended Posts

Reading and writing CSV with Python
Example of reading and writing CSV with Python
Python CSV file reading and writing
Reading and writing NetCDF with Python
Reading and writing csv files
Reading and writing CSV and JSON files in Python
Reading and writing fits files with Python (memo)
[Introduction for beginners] Reading and writing Python CSV files
Reading and writing text in Python
Character code for reading and writing csv files with python ~ windows environment ver ~
Csv tinkering with python
[Python] Reading CSV files
Scraping tabelog with python and outputting to CSV
Read JSON with Python and output as CSV
Programming with Python and Tkinter
Encryption and decryption with Python
Read csv with python pandas
Python and hardware-Using RS232C with Python-
Reading .txt files with Python
Write to csv with Python
python with pyenv and venv
Download csv file with python
Works with Python and R
Reading, displaying and speeding up gifs with python [OpenCV]
Communicate with FX-5204PS with Python and PyUSB
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Make apache log csv with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
[Python] Write to csv file with Python
Scraping with Python, Selenium and Chromedriver
[Python] Read the csv file and display the figure with matplotlib
Output to csv file with Python
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Handle Excel CSV files with Python
Multi-line size specification reading with python
I played with PyQt5 and Python3
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Notes on reading and writing float32 TIFF images in python
Extract bigquery dataset and table list with python and output as CSV
How to import CSV and TSV files into SQLite with Python
uproot: Python / Numpy based library for reading and writing ROOT files
Extract database tables with CSV [ODBC connection from R and python]
Data pipeline construction with Python and Luigi
Calculate and display standard weight with python
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Read CSV file with python (Download & parse CSV file)
Passwordless authentication with RDS and IAM (Python)
Python installation and package management with pip
Using Python and MeCab with Azure Databricks
"System trade starting with Python3" reading memo