Untitled Document
CSV MODULE - Files with delimiters
1. Simple example of or reading , ( comma ) separated CSV file Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****  
       # We use CSV module to parse the data from a comma separated file 
       # CSV Module has a lot of useful feature which can easily handle CSV parsing    
         
       # Program - Simple example of or reading , ( comma ) separated CSV file
       # Below example will use csv.reader module which reeturns an oiterable objects 
       # we can use for loop to iterate through it . The o/p is generated as list and we can use indexing to print if  
       # we want to print any specific field 
        
       import csv 
       	with open('names.csv', 'r') as csv_file:
       		csv_reader = csv.reader(csv_file)
       		for line in csv_reader:
       			print(line)
       			print(line[2]) # --> This will print email field 

       
2. Program - How to skip first line ( Header) in CSV file Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - How to skip first line ( Header) in CSV file
       # we will use next method() to skip the line 
       # the conext managers are easily managable and we don't need to explicitly close the file
       
       import csv
       with open('names.csv', 'r') as csv_file:
       	csv_reader = csv.reader(csv_file)
       	next(csv_reader)
       	for line in csv_reader:
       		print(line)
       
       # IMP - read() method flush entire file contents on terminal ; 
       # Now if you have file have large contents then it is not advisable to use above approach
       
       # Another way of printing lines from file 
       
       with open('test.txt', 'r') as f:
       	f_contents = f.readline()
       	print(f_contents)
       
       # The readline() function will print only one line from a file 
       with open('test.txt', 'r') as f:
       	f_contents = f.readline()
	   	print(f_contents,end='')
	   	f_contents = f.readline()
	   	print(f_contents,end='')
       
       # The above 4 lines will print next 2 lines on terminal ; but again this is not the way to print entire file on termial 
       
       # readlines() - This function will print all the file contents in list 
       
       with open('test.txt', 'r') as f:
       	f_contents = f.readlines()
       	print(f_contents)

       
3. Program - writing data / creating new csv file using csv module Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - writing data / creating new csv file using csv module

       import csv
       	with open('names.csv','r') as csv_file:
       		csv_reader = csv.reader(csv_file)
       		with open('new_names.csv','w') as new_file:
       			csv_writer = csv.writer(new_file, delimiter='-')
       			for line in csv_reader:
       				csv_writer.writerow(line)

       
4. Program - Reading Files other than , (comma) Seperated delimiter Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Reading Files other than , (comma) Seperated delimiter
       # if you have files with other than comma(,) as a delimiter then during 
       # reading such files you need to explicitly pass/mention the delimter to cs reacer
       # other wise your data will be printed in a single line 
       # you can create tab as a delimeter '-' 
       
       import csv
       with open('new_names.csv','r') as csv_file
       	csv_reader = csv.reader(csv_file,delimiter='-')
       	for line in csv_reader:
       		print(line)

       
5. Reading csv file using dictionary reader Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Reading csv file using dictionary reader
       
       import csv 
       with open('new_names.csv','r') as csv_file:            
       	csv_reader = csv.DictReader(csv_file,delimiter='-')
                    
       	for line in csv_reader:
       		print(line)
       		print(line['Email'])
       
       
6. writing / creating new file using csv dictionary writer Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # writing / creating new file using csv dictionary writer

       import csv
       	with open('names.csv','r') as csv_file:            
       		csv_reader = csv.DictReader(csv_file)
       		with open('new_names1.csv','w') as new_file:
       			fieldnames = ['FirstName', 'LastName', 'Email']
       			csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter='\t')
       			csv_writer.writeheader()
       			for line in csv_reader:
       				csv_writer.writerow(line)
 
       
7. Creating one more file using csv Dictionary writer Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Creating one more file using csv Dictionary writer
       # writing / creating new file using csv dictionary writer by removing the not required field .
       # In below example we don't want  email field in new file from the original file
       
       import csv
       with open('names.csv','r') as csv_file:
       	csv_reader = csv.DictReader(csv_file)  
       	with open('new_names2.csv','w') as new_file:
       		fieldnames = ['FirstName', 'LastName']
       		csv_writer.writeheader()
       		for line in csv_reader:
       			del line['Email']
       			csv_writer.writerow(line)
              
       
Untitled Document