天天看點

Python處理CSV 之 二

作者:洪較瘦不着調退役it人

上檔案介紹了CSV的讀寫及基本知識

Python處理CSV檔案

CSV複雜一點的處理

有這樣一個CSV檔案内容如下 姓名 部門 生日 月 4個字段

name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March           

讀取這個檔案後想自定義處理一下輸出

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.           
Python處理CSV 之 二

寫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 引用那些包含特殊字元的字段,例如分隔符、引号字元或任何行終止符中的字元。
  • delimiter 預設情況 是 , 逗号分隔
employee_writer.writerow(['John Smith', 'Accounting', ',November'])           

,November 這個值包含一個逗号

看一下寫入檔案後的效果,整個字元串加了一個引号來包裹,這也是CSV的标準之一。

  • John Smith,Accounting,",November"
Python處理CSV 之 二

CSV檔案首行寫入标題

我們知道通常CSV中第一個會給一個标題行,表示每一行的資料的含義解釋,當然 這不是必要的。隻是看業務上的需求

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'})           

看一下寫入後的檔案效果

Python處理CSV 之 二

使用更冬日的工具 pandas來處理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           
Python處理CSV 之 二
Python處理CSV 之 二

繼續閱讀