打印数据帧显示更新的数据,但是to_csv将旧数据写入CSV文件

时间:2019-04-06 16:50:48

标签: python pandas csv dataframe export-to-csv

在将python代码在数据框中执行一些更新之后,我正在将csv数据读取到数据框中。在打印数据框时,我可以看到更新的值,但是在写回csv时。我仍然在使用旧的价值观。

to_csv is not writing the updated DataFrame

问题与上面的链接相似,但是我只有一个类代码,没有对同一文件的多个引用,但是我仍然遇到相同的问题,并且我对此评论没有足够的声誉。 / p>

import win32com.client as win32
import pandas as pd



class MailDispatcher:

    global file_location
    file_location = 'C:\Pilot Run\Files\\'

    delivery_status = pd.read_csv(file_location + "delivery_status.csv")

    for index, row in delivery_status.iterrows():

        # Mail trigger
        outlook = win32.Dispatch('outlook.application')
        mail = outlook.CreateItem(0)
        mail.To = 'abc@gmail.com'
        mail.Subject = 'test ' + str(row[3])

        if mail.Send() is None:
            row[4] = 'Y'
            print(row) #printing correct data
            outlook.Quit()

    print(delivery_status)
    #output = open(file_location + 'delivery_status.csv', 'w')

# writing old data in csv
    delivery_status.to_csv(file_location + 'delivery_status.csv', index=None, header=True)
    #output.close()

##Tried with the below code to as per some comments, still getting the same issue:

    output = open(file_location + 'delivery_status.csv', 'w')
    delivery_status.to_csv(output, index=None, header=True)
    output.close()

0 个答案:

没有答案