Python进阶 -- 3. 处理CSV格式

CSV文件格式
将数据作为一系列以逗号分隔的值 (CSV)

(1). 分析CSV文件头

import csv
filename = '111.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)
    print(header_row)

(2). 打印文件头及其位置

import csv
filename = '111.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    for index, column_header in enumerate(header_row):
        print(index, column_header)

(3). 提取并读取数据

import csv

filename = '111.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    highs = []
    for row in reader:
        highs.append(row[1])

    print(highs)

你可能感兴趣的:(Python)