需求:從幾百個CSV或xls中讀取某些重要數據,並匯總在一個單獨的excel中進行數據分析
Python實現:
# coding:utf-8 # File Name: csv_data_sort # Description : # Author : micro # Date: 2019/9/17 import glob, os import csv import xlrd, xlwt from xlutils.copy import copy def run(): # 第一步 遍歷讀取文件夾,獲取每個csv的路徑 path = r'C:\Users\micro\Desktop\2018流量' files = glob.glob(os.path.join(path, "*.csv")) # 第二步 分別遍歷每個文件,並獲取所需要網站的UV all_result = [] for file in files: csvFile = open(file, "r") reader = csv.reader(csvFile) for item in reader: if (len(item) > 1): if (item[0] == "www.baidu.com"): xx1 = item[2] if (item[0] == "www.baidu.com"): xx2 = item[2] if (item[0] == 'www.baidu.com'): xx3 = item[2] if (item[0] == "www.baidu.com"): xx4 = item[2] if (item[0] == "www.baidu.com"): xx5 = item[2] if (item[0] == "www.baidu.com"): xx6 = item[2] result = [xx1,xx2,xx3,xx4,xx5,xx6] all_result.append(result) write_excel_xls_append(r"C:\Users\micro\Desktop\2018.xls", all_result) def write_excel_xls_append(path, value): index = len(value) # 獲取需要寫入數據的行數 workbook = xlrd.open_workbook(path) # 打開工作簿 sheets = workbook.sheet_names() # 獲取工作簿中的所有表格 worksheet = workbook.sheet_by_name(sheets[0]) # 獲取工作簿中所有表格中的的第一個表格 rows_old = worksheet.nrows # 獲取表格中已存在的數據的行數 new_workbook = copy(workbook) # 將xlrd對象拷貝轉化為xlwt對象 new_worksheet = new_workbook.get_sheet(0) # 獲取轉化后工作簿中的第一個表格 for i in range(0, index): for j in range(0, len(value[i])): new_worksheet.write(i + rows_old, j, value[i][j]) # 追加寫入數據,注意是從i+rows_old行開始寫入 new_workbook.save(path) # 保存工作簿 print("xls格式表格【追加】寫入數據成功!") if __name__ == '__main__': run()