使用 Python 的 CSV 模块覆盖 csv 文件中的特定行

发布于 2024-10-02 07:13:57 字数 729 浏览 2 评论 0原文

我正在使用 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 帖子,但没有结果。我认为应该归咎于我有限的编程知识而不是谷歌。

I'm using Python's csv module to do some reading and writing of csv files.

I've got the reading fine and appending to the csv fine, but I want to be able to overwrite a specific row in the csv.

For reference, here's my reading and then writing code to append:

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

And I'm using basically the same for the overwrite mode(which isn't correct, it just overwrites the whole csv file):

    b=open('bottles.csv','wb')
    writer = csv.writer(b)
    writer.writerow([bottle,btlnum,100,img])
    b.close()

In the second case, how do I tell Python I need a specific row overwritten? I've scoured Gogle and other stackoverflow posts to no avail. I assume my limited programming knowledge is to blame rather than Google.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说不完的你爱 2024-10-09 07:13:57

我将添加到 史蒂文回答:

import csv

bottle_list = []

# Read all data from the csv file.
with open('a.csv', 'rb') as b:
    bottles = csv.reader(b)
    bottle_list.extend(bottles)

# data to override in the format {line_num_to_override:data_to_write}. 
line_to_override = {1:['e', 'c', 'd'] }

# Write data to the csv file and replace the lines in the line_to_override dict.
with open('a.csv', 'wb') as b:
    writer = csv.writer(b)
    for line, row in enumerate(bottle_list):
         data = line_to_override.get(line, row)
         writer.writerow(data)

I will add to Steven Answer :

import csv

bottle_list = []

# Read all data from the csv file.
with open('a.csv', 'rb') as b:
    bottles = csv.reader(b)
    bottle_list.extend(bottles)

# data to override in the format {line_num_to_override:data_to_write}. 
line_to_override = {1:['e', 'c', 'd'] }

# Write data to the csv file and replace the lines in the line_to_override dict.
with open('a.csv', 'wb') as b:
    writer = csv.writer(b)
    for line, row in enumerate(bottle_list):
         data = line_to_override.get(line, row)
         writer.writerow(data)
东走西顾 2024-10-09 07:13:57

您无法覆盖 CSV 文件中的单行。您必须将所需的所有行写入新文件,然后将其重命名回原始文件名。

您的使用模式可能比 CSV 文件更适合数据库。查看 sqlite3 模块以获得轻量级数据库。

You cannot overwrite a single row in the CSV file. You'll have to write all the rows you want to a new file and then rename it back to the original file name.

Your pattern of usage may fit a database better than a CSV file. Look into the sqlite3 module for a lightweight database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文