zoukankan      html  css  js  c++  java
  • 将csv文件转为xlsx文件,解决csv乱码问题

    工作中,我们经常遇到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运行

  • 相关阅读:
    LeetCode——Basic Calculator
    LeetCode——Sqrt(x)
    LeetCode——Binary Search Tree Iterator
    LeetCode——Search for a Range
    LeetCode——pow(x, n)
    LeetCode——Single Number II
    LeetCode——Summary Ranges
    LeetCode——Largest Number
    LeetCode——Kth Largest Element in an Array
    LeetCode——Implement Stack using Queues
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/13807922.html
Copyright © 2011-2022 走看看