首页 > 解决方案 > 我想将我的目标文件保存为 .CSV 而不是 xlsx

问题描述

我想将我的目标文件保存在 .CSV 而不是 xlsx 中,此代码运行良好,但我无法将输出文件保存为 .CSV 我在 google 上查看,但我找不到任何东西

# Main function
def classement_top(FILE_SOURCE,FILE_DESTINATION):
    df = pd.read_csv(FILE_SOURCE)
    liste_partant = list(set(df['comp']))
    liste_filtre_partant = []
    for partant in liste_partant:
        source = 'df_partcccant_' + str(partant)
        liste_filtre_partant.append(source)
        vars()[source] = df[df['comp'] == partant]
        vars()[source] = vars()[source].nlargest(5, ['total_teste'])
        top1 = 0
        top2 = 0
        top3 = 0
        top4 = 0
        top5 = 0
        for elem in list(vars()[source]['cl']):
            if elem == '1er':
                top1 = 1
            if elem == '2e':
                top2 = 1
            if elem == '3e':
                top3 = 1
            if elem == '4e':
                top4 = 1
            if elem == '5e':
                top5 = 1
        top5 = top5 + top4 + top3 + top2 + top1
        top4 = top4 + top3 + top2 + top1
        top3 = top3 + top2 + top1
        top2 = top2 + top1

        for i in list(vars()[source].index):
            vars()[source].at[i,'top1'] = top1
            vars()[source].at[i,'top2'] = top2
            vars()[source].at[i,'top3'] = top3
            vars()[source].at[i,'top4'] = top4
            vars()[source].at[i,'top5'] = top5

    liste_variable = []
    for elem in liste_filtre_partant:
        liste_variable.append(vars()[elem])

    final_df = pd.concat(liste_variable)
    final_df = final_df.sort_values(by=['jour', 'comp'], ascending=True)
    final_df.to_excel(FILE_DESTINATION, index=False)

使用我们要保存文件的位置启动函数 FILE_DESTINATION 这段代码运行良好,但我无法将输出文件保存为 .CSV

FILE_DESTINATION = "C:/Users/Utilisateur/Documents/PMU/EXPORT/sorti_teste_1_suite.xlsx"

文件源

FILE_SOURCE = "C:/Users/Utilisateur/Documents/PMU/EXPORT/teste_1_suite.csv"
classement_top(FILE_SOURCE,FILE_DESTINATION)

标签: python

解决方案


利用final_df.to_excel(FILE_DESTINATION, index=False)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

(移动评论回答)


推荐阅读