Python read csv fileCSV FileA csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format. Reading CSV filesPython provides various functions to read csv file. Few of them are discussed below as. To see examples, we must have a csv file. Let's consider an example data in the python.csv file as
1. Using csv.reader() functionIn Python, the csv.reader() module is used to read the csv file. It takes each row of the file and makes a list of all the columns. Example: Code Output: [ 'Name', 'Age', 'City' ] [ 'Ram', '27', 'Mumbai' ] [ 'Bheem', '29', 'Pune' ] [ 'Sita', '23', 'Delhi' ] In the above code, we have opened 'python.csv' using the open() function. We used csv.reader() function to read the file, that returns an iterable reader object. The reader object have consisted the data and we iterated using for loop to print the content of each row 2. Read a CSV into a DictionaryWe can also use DictReader() function to read the csv file directly into a dictionary rather than deal with a list of individual string elements. Example: Code Output: The column names are as follows : Name, Age, City Ram lives in Mumbai department and is 27 years old. Bheem lives in Pune department and is 29 years old. Sita lives in Delhi department and is 23 years old. Processed 4 lines. Reading csv files with Pandas3. Reading csv files with PandasThe Pandas is defined as an open-source library which is built on the top of the NumPy library. It provides fast analysis, data cleaning, and preparation of the data for the user. Reading the csv file into a pandas DataFrame is quick and straight forward. We don't need to write enough lines of code to open, analyze, and read the csv file in pandas and it stores the data in DataFrame. Here, we are taking a slightly more complicated file to read, called hrdata.csv, which contains data of company employees. Name, Hire Date, Salary, Leaves Remaining John Idle, 08/15/14, 50000.00, 10 Smith Gilliam, 04/07/15, 65000.00, 8 Parker Chapman, 02/21/14,45000.00, 10 Jones Palin, 10/14/13, 70000.00, 3 Terry Gilliam, 07/22/14, 48000.00, 7 Michael Palin, 06/28/13, 66000.00, 8 Example: Code In the above code, the three lines are enough to read the file, and only one of them is doing the actual work, i.e., pandas.read_csv() Output: Name Hire Date Salary Leaves Remaining 0 John Idle 03/15/14 50000.0 10 1 Smith Gilliam 06/01/15 65000.0 8 2 Parker Chapman 05/12/14 45000.0 10 3 Jones Palin 11/01/13 70000.0 3 4 Terry Gilliam 08/12/14 48000.0 7 5 Michael Palin 05/23/13 66000.0 8 Python CSV Module FunctionsThe CSV module work is used to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:
ConclusionWhile working with CSV records, it is essential to guarantee that the document is appropriately organized and that the fitting libraries are imported. You might have to deal with potential issues, for example, missing qualities, conflicting information types, and extraordinary characters. Understanding the design of the CSV record, including the header and information sections, is pivotal for powerful information handling. Next TopicPython write csv file |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India