In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to read and write CSV files from Python". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope that this article "how to read and write CSV files from Python" can help you solve the problem.
What is a CSV file?
A CSV file (comma-separated value file) is a plain text file that uses a specific structure to arrange table data. Because it is a plain text file, it can only contain actual text data, in other words, printable ASCII or Unicode characters.
The structure of the CSV file is given by its name. Typically, CSV files use commas to separate each specific data value.
Column 1 name,column 2 name,column 3 name1st row data 1,1st row data 2,1st row data 32nd row data 1,2nd row data 2,2nd row data 3
Notice how each piece of data is separated by commas. Usually the first row identifies each piece of data, in other words, the name of the data column. Each subsequent line is actual data and is limited by file size.
Usually the delimiter (,) comma is not the only one used. Other popular delimiters include tab (\ t), colon (:), and semicolon (;) characters.
Parsing the CSV file correctly requires knowing which delimiter is being used.
Where do CSV files come from?
CSV files are usually created by programs that process large amounts of data. They are a convenient way to export data from spreadsheets and databases and to import or use data in other programs. For example, you can export the results of a data mining program as a CSV file, and then import it into a spreadsheet to analyze the data, generate charts for presentation, or prepare for publishing reports.
CSV files are very easy to deal with in Python programming, and you can work with CSV files directly.
The built-in CSV library parses CSV files
The csv library is designed to use CSV files generated by Excel out of the box and adapts to a variety of CSV formats.
Read CSV file csv
The CSV file is opened as a text file using Python's built-in open () function, which returns a file object and passes it to reader for reading.
# employee_birthday.txtname,department,birthdayJohn,IT,NovemberTom,IT,March
Reading the operation code, each line of reader returned is a list of elements, and the String contains the data found by removing the delimiter. The first row returned contains column names that are handled in a special way.
Import csvwith open ('employee_birthday.txt') as csv_file: csv_reader = csv.reader (csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count = = 0: print (f'names are {"," .join (row)}') line_count + = 1 else: print (f'\ t {row [0]} works in the {row [1]} department And was born in {row [2]}.') Line_count + = 1 print (f'Processed {line_count} lines.') names are name, department, birthday John works in the IT department, and was born in November. Tom works in the IT department, and was born in March.Processed 3 lines. Read the CSV file into the dictionary csv
In addition to working with a list of individual String elements, CSV data can be read directly into the dictionary.
Import csvwith open ('employee_birthday.txt', mode='r') as csv_file: csv_reader = csv.DictReader (csv_file) line_count = 0 for row in csv_reader: if line_count = = 0: print (f'Column names are {"," .join (row)}') line_count + = 1 print (f'\ t {row ["name"]} works in the {row ["department"]} department And was born in {row ["birthday month"]}.') Line_count + = 1 print (f'Processed {line_count} lines.') Column names are name, department, birthday John works in the IT department, and was born in November. Tom works in the IT department, and was born in March.Processed 3 lines. Optional Python CSV reader parameter
Delimiter specifies the characters used to separate each field. The default value is comma (',').
Quotechar specifies the character used to enclose the field that contains the delimiter. The default value is double quotes ('").
Escapechar specifies the character used to escape the delimiter in case quotation marks are not used. There are no escape characters by default.
Name,address,date joinedjohn,1132 Anywhere Lane Hoboken NJ, 07030,Jan 4erica,1234 Smith Lane Hoboken NJ, 07030,March 2
This CSV file contains three fields, name, address, and date joined, separated by commas. The problem is that the data in this address field also contains a comma to represent the zip code.
There are three ways to deal with this.
Use a different delimiter and use the delimiter optional parameter to specify the new delimiter.
Enclosing the data in quotation marks, the special nature of the selected delimiter is ignored in the quoted string. Quotechar can use optional parameters to specify the characters used for references.
Delimiters in escaped data, escape characters work in the same way as they do in format strings, invalidating the interpretation of escaped characters (in this case, delimiters). If you use escape characters, you must specify them using the escapechar optional parameter.
Write to a file using csv
You can write to the CSV file using the writer object and the. Write _ row () method.
Import csvwith open ('employee_file.csv', mode='w') as employee_file: employee_writer = csv.writer (employee_file, delimiter=',', quotechar=' ", quoting=csv.QUOTE_MINIMAL) employee_writer.writerow ([' John Smith', 'Accounting',' November']) employee_writer.writerow (['Erica Meyers',' IT', 'March'])
Csv.QUOTE_MINIMAL means only when required, for example, when a field contains either the quotechar or the delimiter
Csv.QUOTE_ALL means that quotes are always placed around fields.
Csv.QUOTE_NONNUMERIC means that quotes are always placed around
Fields which do not parse as integers or floating point numbers.
Csv.QUOTE_NONE means that quotes are never placed around fields.
Csv.QUOTE_MINIMAL: the writer object refers only to those that contain special characters.
Csv.QUOTE_ALL: the writer object references all fields, such as the field delimiter, quotechar, or any character lineterminator.
The csv.QUOTE_NONNUMERIC: the writer object references all non-numeric fields, instructing the reader to convert all non-reference fields to the float type.
Csv.QUOTE_NONE: the writer object does not reference fields, if escapechar error throw is not set; indicates that reader does not perform special handling on quotation mark characters.
John Smith,Accounting,NovemberErica Meyers,IT,March writes the CSV file csv from the dictionary
The DictReader parameter is required when writing a dictionary.
Import csvwith open ('employee_file2.csv', mode='w') as csv_file: fieldnames= [' emp_name', 'dept',' birth_month'] writer = csv.DictWriter (csv_file, fieldnames=fieldnames) writer.writeheader () writer.writerow ({'emp_name':' John Smith', 'dept':' Accounting', 'birth_month':' November'}) writer.writerow ({'emp_name':' Erica Meyers') 'dept':' IT', 'birth_month':' March'}) use the pandas library to parse CSV files
You can install the pandas library first.
Pip install pandaspandas reads CSV file # hrdata.csvName,Hire Date,Salary,Sick Days remainingGraham Chapman,03/15/14,50000.00,10John Cleese,06/01/15,65000.00,8Eric Idle,05/12/14,45000.00,10Terry Jones,11/01/13,70000.00,3Terry Gilliam,08/12/14,48000.00,7Michael Palin,05/23/13,66000.00,8
Use pandas to read the csv file.
Import pandas as pddf = pd.read_csv ('hrdata.csv') print (df) Name Hire Date Salary Sick Days remaining0 Graham Chapman 03 Terry Jones 15 + 14 50000.0 101 John Cleese 06 * 01 * 34 Terry Gilliam 08/12/14 48000.0 75 Michael Palin 05/23/13 66000.0 8
Add an index column to read the csv file so that the index number is gone.
Import pandas as pddf = pd.read_csv ('hrdata.csv' Index_col='Name') print (df) Hire Date Salary Sick Days remainingName Graham Chapman 03ax 15 10Terry Jones 14 50000.0 10John Cleese 06 Grey 01 15 65000.0 8Eric Idle 05 Grey 12 14 45000.0 10Terry Jones 11-01-13 70000.0 3Terry Gilliam 08/12/14 48000.0 7Michael Palin 05/23/13 66000.0 8
Fixed that the data type of the Hire Date field is date data.
Import pandas as pddf = pd.read_csv ('hrdata.csv', index_col='Name' Parse_dates= ['Hire Date']) print (df) Hire Date Salary Sick Days remainingName Graham Chapman 2014-03-15 50000.0 10John Cleese 2015-06-01 65000.0 8Eric Idle 2014-05-12 45000.0 10Terry Jones 2013-11 -01 70000.0 3Terry Gilliam 2014-08-12 48000.0 7Michael Palin 2013-05-23 66000.0 8
It can also be dealt with in a unified manner.
Import pandas as pddf = pd.read_csv ('hrdata.csv', index_col='Employee', parse_dates= [' Hired'], header=0, names= ['Employee',' Hired','Salary') 'Sick Days']) print (df) Hired Salary Sick DaysEmployee Graham Chapman 2014-03-15 50000.0 10John Cleese 2015-06-01 65000.0 8Eric Idle 2014-05-12 45000.0 10Terry Jones 2013-11-01 70000.0 3Terry Gilliam 2014-08-12 48000.0 7Michael Palin 2013-05-23 66000.0 8pandas writes to CSV file
Write operations are as simple as read operations.
Import pandas as pddf = pd.read_csv ('hrdata.csv', index_col='Employee', parse_dates= [' Hired'], header=0, names= ['Employee',' Hired', 'Salary',' Sick Days']) df.to_csv ('hrdata_modified.csv') on "how Python reads and writes CSV files" ends here. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.