通过python转CSV到Excel

# --*-- coding=utf-8 --*--

import csv
from buy.model import Knowledge

if __name__ == "__main__":
    list = []
    try:
        with open("d://aaa.csv", "r", encoding="utf-8") as f:
            csv_reader = csv.reader(f)
            birth_header = next(csv_reader)
            for row in csv_reader:
                knowledge = Knowledge.Knowledge(str(row[13]).lstrip('["').rstrip('"]'), str(row[4]).strip('"'))
                list.append(knowledge)
    except Exception as ex:
        print("当前发生了错误,", ex)

    if list is not None and list.__len__() > 0:
        import xlwt

        index = 1
        workbook = xlwt.Workbook()
        worksheet = workbook.add_sheet('knowledge')
        worksheet.write(0, 0, "知识标题(必填)")
        worksheet.write(0, 1, "知识内容(必填)")
        for item in list:
            worksheet.write(index, 0, item.title)
            worksheet.write(index, 1, item.content)
            index += 1
        workbook.save('d://xxx.xls')

替换对象:

# --*-- coding=utf-8 --*--

class Knowledge(object):

    def __init__(self, title, content):
        self.title = title
        self.content = content

你可能感兴趣的:(python)