gpt4 book ai didi

python - 如何在类中使用 python csv 模块

转载 作者:行者123 更新时间:2023-11-28 19:15:27 24 4
gpt4 key购买 nike

我正在尝试在类的 init 方法中实例化一个 csv writer,我想从类方法 storeToCSV() 中使用它。由于某些奇怪的原因,这不起作用。当我调用 self.storeToCSV(['test']) 时,没有数据写入文件。这是什么原因,我该如何解决?

class postprocessing:
def __init__(self,topic):

try:
os.remove(topic+'.csv')
except:
pass

csvfile = open(topic+'.csv', 'a')
self.csvwriter = csv.writer(csvfile, delimiter=';',quotechar='"')
self.csvwriter.writerow(['id','url1'])

def storeToCSV(self, arg_a):
print self.csvwriter.writerow(arg_a)

最佳答案

我认为你需要添加一个

csvfile.close()

写入数据后。

关于python - 如何在类中使用 python csv 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com