Created
January 21, 2017 08:23
-
-
Save thombashi/e984ffb5c3bd7b6248b6351f7c9a3d6e to your computer and use it in GitHub Desktop.
Read a CSV file with Python 2/3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Sample program to read a CSV file. | |
This works for both Python 2/3. | |
""" | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
import argparse | |
import csv | |
import io | |
import six | |
parser = argparse.ArgumentParser() | |
parser.add_argument("csv_file_path") | |
parser.add_argument("--encoding", default="utf_8") | |
options = parser.parse_args() | |
csv_reader = csv.reader( | |
io.open(options.csv_file_path, "r", encoding=options.encoding), | |
delimiter=",", | |
quotechar='"' | |
) | |
print("--- header ---\n{}\n".format(six.next(csv_reader))) | |
print("--- data ---") | |
for row in csv_reader: | |
print(row) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment