Python Read CSV File

import csv

src_set = []
file_name = 'coffee.csv'

def read_csv():
    file_path = file_name
    with open(file_path, "r", encoding="utf-8") as fr:
        data = csv.reader(fr)
        for row in data:
            src_set.append(row[0])

Python读写xls表格内容

xlrd:读取xls内容

xlwt:内容写到xls。

import xlrd
import xlwt

src_set = []
dst_set = []
file_name = "Reverse"

def read_xls():
    file_path = file_name + ".xls"
    xls_file = xlrd.open_workbook(file_path)
    xls_sheet1 = xls_file.sheets()[0]  #第一个sheet
    nrows = xls_sheet1.nrows  #行数
    for index in range(nrows):
        row_value = xls_sheet1.cell(index, 0)  #第几行第一列
        src_set.append(str(row_value.value))

def write_xls():
    work_book = xlwt.workbook(encoding='utf-8')
    sheet = work_book.add_sheet("result")  #增加sheet
    index = 0
    for result in dst_set:
        print(result)
        sheet.write(index, 0, result[0])  #第几行第一列
        sheet.write(index, 1, result[1])  #第几行第二列

    work_book.save(file_name + "_result_"  + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".xls")
  

注意:不支持xlsx格式,可以先把xlsx另存为xls格式。