【发布时间】:2010-11-10 20:30:23
【问题描述】:
我正在使用 Python 的 csv 模块来读取和写入 csv 文件。
我已经很好地阅读并附加到 csv 中,但我希望能够覆盖 csv 中的特定行。
作为参考,这是我的阅读然后编写附加代码:
#reading
b = open("bottles.csv", "rb")
bottles = csv.reader(b)
bottle_list = []
bottle_list.extend(bottles)
b.close()
#appending
b=open('bottles.csv','a')
writer = csv.writer(b)
writer.writerow([bottle,emptyButtonCount,100, img])
b.close()
我使用的覆盖模式基本相同(这是不正确的,它只是覆盖了整个 csv 文件):
b=open('bottles.csv','wb')
writer = csv.writer(b)
writer.writerow([bottle,btlnum,100,img])
b.close()
在第二种情况下,我如何告诉 Python 我需要覆盖特定的行?我搜索了 Gogle 和其他 stackoverflow 帖子,但无济于事。我认为应该归咎于我有限的编程知识而不是 Google。
【问题讨论】: