打印数据帧显示更新的数据,但tou csv是将旧数据写入csv-fi

2024-04-18 07:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

to_csv is not writing the updated DataFrame

这个问题与上面的链接类似,但是我只有一个类代码,没有对同一个文件的多个引用,我仍然得到同样的问题,我没有足够的声誉来评论这篇文章。

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()

Tags: csvtheto数据noneoutputindexas