1. 程式人生 > >python——CSV轉Excel

python——CSV轉Excel

install module open utf-8 med pre 轉換 之前 read

在轉換之前,事先需要將csv文件另存為此格式

技術分享圖片

import pandas as pd

def csv_to_xlsx_pd():
    csv = pd.read_csv(rC:\Users\Jery\Desktop\guazi1.csv, encoding=utf-8)
    csv.to_excel(rC:\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(rC:\Users\Jery\Desktop\guazi1.csv, encoding=utf-8)
    csv.to_excel(rC:\Users\Jery\Desktop\guazi1.xlsx, sheet_name=data)


if __name__ == __main__:
    csv_to_xlsx_pd()

python——CSV轉Excel