将数据写入CSV

时间:2019-04-25 10:45:09

标签: python-3.x

我正在尝试将win32_process输出的输出写入csv

def get_Process():
    #for process in c.win32_Process():
    for process in c.win32_Process (name="notepad.exe"):
        with open('D:\writeData.csv', mode='w') as file:
            writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        #print(process.ProcessId,process.Name)
            writer.writerow(['Process Name','Process ID'])
            writer.writerow(['process.Name','process.processId'])

当我执行代码时,它没有给我任何错误,但没有创建csv文件,因此也无法编写。

请让我知道我在做什么错

2 个答案:

答案 0 :(得分:0)

def get_Process():
    #for process in c.win32_Process():
    for process in c.win32_Process(name="notepad.exe"):
        with open('D:\writeData.csv', mode='w') as file:
            writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        #print(process.ProcessId,process.Name)
            writer.writerow(['Process Name','Process ID'])
            writer.writerow([process.Name,process.processId])

应该做你想做的。请注意,我将第二次写入从字符串更改为实际对象。

答案 1 :(得分:-1)


csvData = [['Person','Age'],['Peter','22'],['Jasmine', '21'],['Sam', '24']]

with open('person.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(csvData)
csvFile.close