Thursday, July 9, 2020

How to Read a CSV File in Python

How to Read a CSV File in Python How to Read CSV File in Python? Back Home Categories Online Courses Mock Interviews Webinars NEW Community Write for Us Categories Artificial Intelligence AI vs Machine Learning vs Deep LearningMachine Learning AlgorithmsArtificial Intelligence TutorialWhat is Deep LearningDeep Learning TutorialInstall TensorFlowDeep Learning with PythonBackpropagationTensorFlow TutorialConvolutional Neural Network TutorialVIEW ALL BI and Visualization What is TableauTableau TutorialTableau Interview QuestionsWhat is InformaticaInformatica Interview QuestionsPower BI TutorialPower BI Interview QuestionsOLTP vs OLAPQlikView TutorialAdvanced Excel Formulas TutorialVIEW ALL Big Data What is HadoopHadoop ArchitectureHadoop TutorialHadoop Interview QuestionsHadoop EcosystemData Science vs Big Data vs Data AnalyticsWhat is Big DataMapReduce TutorialPig TutorialSpark TutorialSpark Interview QuestionsBig Data TutorialHive TutorialVIEW ALL Blockchain Blockchain TutorialWhat is BlockchainHyperledger FabricWhat Is EthereumEthereum TutorialB lockchain ApplicationsSolidity TutorialBlockchain ProgrammingHow Blockchain WorksVIEW ALL Cloud Computing What is AWSAWS TutorialAWS CertificationAzure Interview QuestionsAzure TutorialWhat Is Cloud ComputingWhat Is SalesforceIoT TutorialSalesforce TutorialSalesforce Interview QuestionsVIEW ALL Cyber Security Cloud SecurityWhat is CryptographyNmap TutorialSQL Injection AttacksHow To Install Kali LinuxHow to become an Ethical Hacker?Footprinting in Ethical HackingNetwork Scanning for Ethical HackingARP SpoofingApplication SecurityVIEW ALL Data Science Python Pandas TutorialWhat is Machine LearningMachine Learning TutorialMachine Learning ProjectsMachine Learning Interview QuestionsWhat Is Data ScienceSAS TutorialR TutorialData Science ProjectsHow to become a data scientistData Science Interview QuestionsData Scientist SalaryVIEW ALL Data Warehousing and ETL What is Data WarehouseDimension Table in Data WarehousingData Warehousing Interview QuestionsData warehouse architectureTalend T utorialTalend ETL ToolTalend Interview QuestionsFact Table and its TypesInformatica TransformationsInformatica TutorialVIEW ALL Databases What is MySQLMySQL Data TypesSQL JoinsSQL Data TypesWhat is MongoDBMongoDB Interview QuestionsMySQL TutorialSQL Interview QuestionsSQL CommandsMySQL Interview QuestionsVIEW ALL DevOps What is DevOpsDevOps vs AgileDevOps ToolsDevOps TutorialHow To Become A DevOps EngineerDevOps Interview QuestionsWhat Is DockerDocker TutorialDocker Interview QuestionsWhat Is ChefWhat Is KubernetesKubernetes TutorialVIEW ALL Front End Web Development What is JavaScript รข€" All You Need To Know About JavaScriptJavaScript TutorialJavaScript Interview QuestionsJavaScript FrameworksAngular TutorialAngular Interview QuestionsWhat is REST API?React TutorialReact vs AngularjQuery TutorialNode TutorialReact Interview QuestionsVIEW ALL Mobile Development Android TutorialAndroid Interview QuestionsAndroid ArchitectureAndroid SQLite DatabaseProgramming How to Read CSV File in Python, we will be learning how to read, write and parse a CSV file in Python.The following aspects will be discussed in detail:What is a CSV File and its uses?Why is CSV File format used?Python CSV moduleCSV module functionsOperations to perform Write, read a CSV file in PythonLets get started.What is a CSV File and its uses?A CSV(Comma Separated Values) is a plain-text file format used to store tabular data such as a spreadsheet or a database. It essentially stores a tabular data which comprises of numbers and text into plain text. Most of the online services give users the liberty to export data from the website into CSV file format. CSV Files generally open into Excel and nearly all the databases have different specific tools to allow the import of the same.Every line of the file is called a record. And each record consists of fields that are separated by commaswhich are also known as delimiter which is the default delimiter, others include pipe(|), semicolon(;). Given below is a structure of a Normal CSV File separated by a comma, I am making use of a titanic CSV file.StructurePassenger,Id,Survived,Pclass,Name,Sex.Age 1,0,3 Braund, Mr. Owen Harris ,male, 22 2,1,1 Cumings, Mrs. John Bradley (Florence Briggs Thayer), female,38 3,1,3 Heikkinen, Miss. Laina ,female, 26 4,1,1 Futrelle, Mrs. Jacques Heath (Lily May Peel),female,35Moving on lets talk about the reason behind usage of CSV File format. Why is CSV File Format Used?CSV is a plain-text file which makes it easier for data interchange and also easier to import onto spreadsheet or database storage. For example: You might want to export the data of a certain statistical analysis to CSV file and then import it to the spreadsheet for further analysis. Overall it makes users working experience very easy programmatically. Any language supporting a text file or string manipulation like Python can work with CSV files directly. Moving ahead, lets see how Python natively uses CSV.Python CSV modulePython uses a CSV package which is a part of the standard library, so you need not install it. import csv Now let me show you the different CSV functions.CSV Module FunctionsUnder the CSV module, you can find the following functions:FunctionsDescriptioncsv.field_size_limitIt returns the maximum field sizecsv.get_dialectFetches the dialect associated with namecsv.list_dialectsDisplays all the registered dialectscsv.readerRead data from csv filecsv.register_dialectDialect associated with a namecsv.writerWrites data to a csv filecsv.unregister_dialectIt deletes the dialect associated with the name dialect registrycsv.QUOTE_ALLQuotes everything irrespective of the typecsv.QUOTE_MINIMALQuotes special character fieldcsv.QUOTE_NONNUMERICQuotes fields that are not numeralcsv.QUOTE_NONE Doesnt quote anything in outputLets move ahead and see from the coding perspective of the different operations on the CSV file in Python.Operations On CSV file in PythonYou can perform several manipulations once a CSV file is loaded. I am going to show the read and write operations on a CSV file in Python.Read CSV file in Python: import csv with open('Titanic.csv','r') as csv_file: #Opens the file in read mode csv_reader = csv.reader(csv_file) # Making use of reader method for reading the file for line in csv_reader: #Iterate through the loop to read line by line print(line) Output: Here, as you can see from the output, I have made use of Titanic CSV File. And all the fields are separated by a comma, File is read into Python.Moving ahead, lets see how you can write to a CSV file.Write to CSV file in Python: import csv with open('Titanic.csv', 'r') as csv_file: csv_reader = csv.reader(csv_file) with open('new_Titanic.csv', 'w') as new_file: # Open a new file named 'new_titanic.csv' under write mode csv_writer = csv.writer(new_file, delimiter=';') #making use of write method for line in csv_reader: # for each file in csv_reader csv_writer.writerow(line) #writing out to a new file from each line of the original file Output: Now this way of working with CSV file using a reader and writer method is one of the most common approaches. Lets move on and see how you can do the same thing using a python dictionary. Read CSV file as Dictionary: import csv with open('Titanic.csv','r') as csv_file: #Open the file in read mode csv_reader = csv.DictReader(csv_file) #use dictreader method to reade the file in dictionary for line in csv_reader: #Iterate through the loop to read line by line print(line) Output: As you can see from the output, field has been replaced and they now act as a key of dictionary.Lets see how we can write to a CSV file as dictionary.Write to CSV file as Dictionary import csv mydict = [{'Passenger':'1', 'Id':'0', 'Survived':'3'}, #key-value pairs as dictionary obj {'Passenger':'2', 'Id':'1', 'Survived':'1'}, {'Passenger':'3', 'Id':'1', 'Survived':'3'}] fields = ['Passenger', 'Id', 'Survived'] #field names filename = 'new_Titanic.csv' #name of csv file with open('new_Titanic.csv', 'w')as new_csv_file: #open a new file 'new_titanic,csv' under write mode writer = csv.DictWriter(new_csv_file, fieldnames=fields) writer.writeheader() #writing the headers(field names) writer.writerows(mydict) #writing data rows Output: Lets see how to read a CSV file in python as pandas.Read CSV file as Pandas: import pandas #install pandas package result = pandas.read_csv('Titanic.csv') #read the csv file print(result) # print result Output: This brings us to the end of our article How to read CSV File in Python. I hope you are clear with all the concepts related to CSV, how to read and write it, how to read and write CSV as a dictionary and how to read CSV as pandas.Make sure you practice as much as possible and revert your experience.Got a question for us? Please mention it in the comments section of this How to read CSV File in Python article and we will get back to you as soon as possible. To get in-depth knowledge of Python along with its various applications, you canenroll herewith our live online training with 24/7 support and lifetime access.Recommended videos for you Mastering Python : An Excellent tool for Web Scraping and Data Analysis Watch Now Know The Science Behind Product Recommendation With R Programming Watch Now Linear Regression With R Watch Now Python Loops While, For and Nested Loops in Python Programming Watch Now Application of Clustering in Data Science Using Real-Time Examples Watch Now Data Science : Make Smarter Business Decisions Watch Now 3 Scenarios Where Predictive Analytics is a Must Watch Now Diversity Of Python Programming Watch Now Python Programming Learn Python Programming From Scratch Watch Now Introduction to Business Analytics with R Watch Now Python Tutorial All You Need To Know In Python Programming Watch Now Android Development : Using Android 5.0 Lollipop Watch Now Python for Big Data Analytics Watch Now Machine Learning With Python Python Machine Learning Tutorial Watch Now The Whys and Hows of Predictive Modelling-I Watch Now Python List, Tuple, String, Set And Dictonary Python Sequences Watch Now Python Numpy Tutorial Arrays In Python Watch Now The Whys and Hows of Predictive Modeling-II Watch Now Sentiment Analysis In Retail Domain Watch Now Web Scraping And Analytics With Python Watch NowRecommended blogs for you Statistical Modeling in Business Analytics with R Read Article Data Scientist Salary How Much Does A Data Scientist Earn? R ead Article Top Python developer Skills you need to know Read Article What is the use of self in Python? Read Article What is Zip and UnZip Function in Python? Read Article What are Generators in Python and How to use them? Read Article Why Should you go for Python? Read Article How To Implement Linear Discriminant Analysis in R? Read Article What is Polymorphism in OOPs programming? Read Article What is Overfitting In Machine Learning And How To Avoid It? Read Article How to implement Data Structures and Algorithms in Python Read Article How to Implement Decorators in Python? Read Article Data Science Career Opportunities: Your Guide To Unlocking Top Data Scientist Jobs Read Article Python: Interesting Facts You Need To Know Read Article Threading In Python: Learn How To Work With Threads In Python Read Article What are Lambda Functions and How to Use Them? Read Article Top Deep Learning Interview Questions You Must Know in 2020 Read Article AI vs Machine Learning vs Deep Learning Read Article Hash Tables and Hashmaps in Python: What are they and How to implement? Read Article  How To Implement Expert System in Artificial Intelligence? Read Article Comments 0 Comments Trending Courses in Data Science Python Certification Training for Data Scienc ...66k Enrolled LearnersWeekend/WeekdayLive Class Reviews 5 (26150)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.