Created
November 11, 2019 12:27
-
-
Save ashishmadeti/d61855d662398d2a417284542d5b5ee7 to your computer and use it in GitHub Desktop.
Reading a CSV in Python 3
This file contains 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
import csv | |
path_to_csv = input() # Take absolute path to the CSV file as input | |
# Consider the following CSV for the purpose of this example | |
# ID,Name | |
# 1.1,State Bank of India | |
# 1.2.1,K. L. Nair | |
# 1.2.2,Joyce Saldanha | |
# 1.3.1,Jagdish Singh | |
# 1.3.2,Neeta Singh | |
with open(path_to_csv, 'r') as f: | |
csv_reader = csv.DictReader(f) | |
for row in csv_reader: | |
data = dict(row) # Convert the row into a python dictionary | |
print(data['ID']) # Prints '1.1' in first iteration, '1.2.1' in second iteration and so on | |
print(data['Name']) # Prints 'State Bank of India' in first iteration, 'K. L. Nair' in second iteration and so on |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment