工作中,我们经常遇到CSV乱码的问题,这个时候我们可以使用notepad++处理,当然我们可以使用python处理,代码入下
#coding=utf-8 import pandas as pd import os df=open('2_wsg_sswl_loan_rejected_tues_fri.csv',encoding='utf-8').readlines() result=[] for i in df: a,b,c=i.split(',') c=c.strip(' ') d=c.replace('*月*日',b) result.append([a,d]) pd.DataFrame(result).to_csv('2_wsg_sswl_loan_rejected_tues_fri.csv',index=None,header=None,encoding='utf-8_sig') # print('表2更新完成') def csv_to_xlsx(file_csv,file_xlsx): try: csv = pd.read_csv(file_csv) csv.to_excel(file_xlsx,sheet_name='data',encoding='utf-8_sig',header=None,index=None) except : print(file_csv+'表没有数据,已删除') csv_to_xlsx('1_cgm_sswl_not_loan_have_conpon.csv','xxx.xlsx') csv_to_xlsx('2_wsg_sswl_loan_rejected_tues_fri.csv','xxx.xlsx') csv_to_xlsx('3_wsg_yesterday_visitsswl_unapply_act_tues_fri.csv','xxx.xlsx') csv_to_xlsx('4_wsg_yesterday_recieve_coupon_not_use_tues_fri.csv','xxx.xlsx') csv_to_xlsx('5_wsg_sswl_coupon_tomorrow_lapse_mon_thu.csv','xxx.xlsx') os.remove('1_cgm_sswl_not_loan_have_conpon.csv') os.remove('2_wsg_sswl_loan_rejected_tues_fri.csv') os.remove('3_wsg_yesterday_visitsswl_unapply_act_tues_fri.csv') os.remove('4_wsg_yesterday_recieve_coupon_not_use_tues_fri.csv') os.remove('5_wsg_sswl_coupon_tomorrow_lapse_mon_thu.csv') print('已经全部转化为xlsx')
可以将py脚本和需要转换的CSV文件放在一起,然后打开cmd运行