python——CSV转Excel

在转换之前,事先需要将csv文件另存为此格式

import pandas as pd

def csv_to_xlsx_pd():
    csv = pd.read_csv(r'C:\Users\Jery\Desktop\guazi1.csv', encoding='utf-8')
    csv.to_excel(r'C:\Users\Jery\Desktop\guazi1.xlsx', sheet_name='data')


if __name__ == '__main__':
    csv_to_xlsx_pd()

若转换出现  ModuleNotFoundError: No module named 'openpyxl'

请先在cmd里  pip install openpyxl

import pandas as pd
import openpyxl

def csv_to_xlsx_pd():
    csv = pd.read_csv(r'C:\Users\Jery\Desktop\guazi1.csv', encoding='utf-8')
    csv.to_excel(r'C:\Users\Jery\Desktop\guazi1.xlsx', sheet_name='data')


if __name__ == '__main__':
    csv_to_xlsx_pd()

 

posted @ 2019-05-03 00:06  铃铃铃  阅读(12877)  评论(0编辑  收藏  举报