import csv
#----------------------------------------------------------------------
def csv_writer(data, path):
"""
Write data to a CSV file path
"""
#with open(path, "wb") as csv_file:
"""
Append data to a CSV file path
"""
with open(path, "a+") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
writer.writerow(line)
#----------------------------------------------------------------------
def csv_dict_reader(file_obj):
"""
Read a CSV file using csv.DictReader
"""
reader = csv.DictReader(file_obj, delimiter=',')
for line in reader:
print(line["first_name"]),
print(line["last_name"]),
print(line["city"])
if (line["first_name"] == "Naga"):
print "############### CSV FILE SCAN DEMO #################################################"
print ('Now seach the value for' + " " +line["first_name"])
print ('corresponding value for' + ' ' + line["first_name"] + ":" + line["last_name"])
print "############### FOUND VALUE FROM SCAN. END #########################################"
def csv_dict_writer(path, fieldnames, data):
"""
Writes a CSV file using DictWriter
"""
with open(path, "wb") as out_file:
writer = csv.DictWriter(out_file, delimiter=',', fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow(row)
#----------------------------------------------------------------------
if __name__ == "__main__":
print "#################### EXAMPLE 1 ######################"
print "Writing into output.csv File"
data = ["first_name,last_name,city".split(","),
"Neelkanth,Reddy,India".split(","),
"Naga,Surekha,India".split(","),
]
path = "output.csv"
csv_writer(data, path)
print "#####################################################"
print "Reading from output.csv File"
print "#####################################################"
with open("output.csv") as f_obj:
csv_dict_reader(f_obj)
print "#####################################################\n"
print "#################### EXAMPLE 2 ######################"
data = ["first_name,last_name,city".split(","),
"Tyrese,Hirthe,Strackeport".split(","),
"Jules,Dicki,Lake Nickolasville".split(","),
"Dedric,Medhurst,Stiedemannberg".split(","),
"Neelkanth,Reddy,India".split(",")
]
my_list = []
fieldnames = data[0]
for values in data[1:]:
inner_dict = dict(zip(fieldnames, values))
my_list.append(inner_dict)
path = "output_1.csv"
csv_dict_writer(path, fieldnames, my_list)
with open("output_1.csv") as f_obj:
csv_dict_reader(f_obj)
#----------------------------------------------------------------------
def csv_writer(data, path):
"""
Write data to a CSV file path
"""
#with open(path, "wb") as csv_file:
"""
Append data to a CSV file path
"""
with open(path, "a+") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
writer.writerow(line)
#----------------------------------------------------------------------
def csv_dict_reader(file_obj):
"""
Read a CSV file using csv.DictReader
"""
reader = csv.DictReader(file_obj, delimiter=',')
for line in reader:
print(line["first_name"]),
print(line["last_name"]),
print(line["city"])
if (line["first_name"] == "Naga"):
print "############### CSV FILE SCAN DEMO #################################################"
print ('Now seach the value for' + " " +line["first_name"])
print ('corresponding value for' + ' ' + line["first_name"] + ":" + line["last_name"])
print "############### FOUND VALUE FROM SCAN. END #########################################"
def csv_dict_writer(path, fieldnames, data):
"""
Writes a CSV file using DictWriter
"""
with open(path, "wb") as out_file:
writer = csv.DictWriter(out_file, delimiter=',', fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow(row)
#----------------------------------------------------------------------
if __name__ == "__main__":
print "#################### EXAMPLE 1 ######################"
print "Writing into output.csv File"
data = ["first_name,last_name,city".split(","),
"Neelkanth,Reddy,India".split(","),
"Naga,Surekha,India".split(","),
]
path = "output.csv"
csv_writer(data, path)
print "#####################################################"
print "Reading from output.csv File"
print "#####################################################"
with open("output.csv") as f_obj:
csv_dict_reader(f_obj)
print "#####################################################\n"
print "#################### EXAMPLE 2 ######################"
data = ["first_name,last_name,city".split(","),
"Tyrese,Hirthe,Strackeport".split(","),
"Jules,Dicki,Lake Nickolasville".split(","),
"Dedric,Medhurst,Stiedemannberg".split(","),
"Neelkanth,Reddy,India".split(",")
]
my_list = []
fieldnames = data[0]
for values in data[1:]:
inner_dict = dict(zip(fieldnames, values))
my_list.append(inner_dict)
path = "output_1.csv"
csv_dict_writer(path, fieldnames, my_list)
with open("output_1.csv") as f_obj:
csv_dict_reader(f_obj)