The above document describes the reading and writing of CSV and the basics
Python processes CSV files
CSV is a bit more complicated
There is such a CSV file with the following contents: Name, Department, Birthday, Month, 4 fields
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March
After reading this file, I want to customize the output
import csv
with 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 month
John Smith works in the Accounting department, and was born in November.
Erica Meyers works in the IT department, and was born in March.
Processed 3 lines.
Complex example of writing SSV
import csv
with 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'])
employee_writer = csv.writer(employee_file,
delimiter=',',
quotechar='"',
quoting=csv.QUOTE_MINIMAL)
- .csv. QUOTE_MINIMA Reference those fields that contain special characters, such as delimiters, quotation mark characters, or characters in any line terminator.
- The default delimiter is comma-separated
employee_writer.writerow(['John Smith', 'Accounting', ',November'])
, November This value contains a comma
If you look at the effect after writing to the file, the whole string is wrapped in quotation marks, which is also one of the standards for CSV.
- John Smith,Accounting,",November"
The header is written in the first row of the CSV file
We know that usually the first one in CSV will give a header row that indicates the meaning of the data in each row, but of course this is not necessary. Just look at the needs of the business
import csv
with 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'})
Take a look at the file after writing
Use a more wintery tool, pandas, to handle CSV
import pandas
df = pandas.read_csv('hrdata.csv')
print(df)
emp_name dept birth_month
0 John Smith Accounting November
1 Erica Meyers IT March