Python3 CSV操作

import csv
import os
import zipfile


def append_csv(file_path, add_info):
    # 新增的数据行,以列表的形式表示
    # add_info = ["Guo", 150]
    csvFile = open(file_path, "a", encoding="UTF-8", newline='')
    writer = csv.writer(csvFile)
    writer.writerow(add_info)
    csvFile.close()


def read_csv_map_0_1(file_path):
    # 读取csv至字典
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)

    # 建立空字典
    result = {}
    for item in reader:
        if len(item) > 1:
            result[item[0]] = item[1]

    csvFile.close()
    return result


def read_csv_map_0_2(file_path):
    # 读取csv至字典
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)

    # 建立空字典
    result = {}
    for item in reader:
        if len(item) > 2:
            result[item[0]] = item[2]

    csvFile.close()
    return result


def read_csv_map_x_x(file_path, one, two):
    # 读取csv至字典
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)

    # 建立空字典
    result = {}
    for item in reader:
        if len(item) > two:
            result[item[one]] = item[two]

    csvFile.close()
    return result


def read_csv_list_0(file_path):  # 读取第一列
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)
    result = []
    for item in reader:
        if len(item) > 0:
            if item[0]:
                result.append(item[0])
    csvFile.close()
    return result


def read_csv_list_2(file_path):  # 读取第三列
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)
    result = []
    for item in reader:
        if len(item) > 2:
            if item[2]:
                result.append(item[2])
    csvFile.close()
    return result


def read_csv_list_1(file_path):  # 读取第二列
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)
    result = []
    for item in reader:
        if len(item) > 1:
            if item[1]:
                result.append(item[1])
    csvFile.close()
    return result


def read_csv_list_one_line_by_first(file_path, key):
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)
    result = []
    for item in reader:
        if len(item) > 0:
            if item[0] == key:
                for num in range(1, len(item)):
                    result.append(item[num])
    csvFile.close()
    return result


def read_csv_all(file_path):
    csvFile = open(file_path, "r", encoding="UTF-8")
    reader = csv.reader(csvFile)
    result = []
    for item in reader:
        result.append(item)
    return result


def replace_one_line_by_key_0(file_path, line_new):
    res = []
    csvreadfile = open(file_path, 'r')
    reader = csv.reader(csvreadfile)
    for line in reader:
        res.append(line)
    csvreadfile.close()

    num = -1
    for new in range(len(res)):
        one_line_old = res[new]
        print(one_line_old[0] + "||" + line_new[0])
        if one_line_old[0] == line_new[0]:
            print(new)
            num = new
    if num != -1:
        res[num] = line_new

    csvfile = open(file_path, 'w')
    writer = csv.writer(csvfile)
    writer.writerows(res)
    csvfile.close()

你可能感兴趣的:(Python3 CSV操作)