[英]How to Read and Write to the Same csv File?
I am trying to read and write to the same csv file, to specifically delete a row in a csv file if a certain header contains a value.我正在尝试读取和写入同一个 csv 文件,如果某个 header 包含一个值,则专门删除 csv 文件中的一行。 My csv file looks like this:
我的 csv 文件如下所示:
Item #![]() |
Price![]() |
Quantity![]() |
Name![]() |
---|---|---|---|
1 ![]() |
2.99 ![]() |
1 ![]() |
Muffin 1![]() |
2 ![]() |
3.99 ![]() |
2 ![]() |
Muffin 2![]() |
3 ![]() |
4.99 ![]() |
3 ![]() |
Cookie 1![]() |
4 ![]() |
5.99 ![]() |
4 ![]() |
Cookie 2![]() |
I have the following code:我有以下代码:
def delete_item(self, item_num):
self.result[:] = [d for d in self.result if d.get("Item #") != int(item_num)]
input = open('items2.csv', 'rb')
output = open('items2.csv', 'wb')
writer = csv.writer(output)
for row in csv.reader(input):
if row[0]!=item_num:
writer.writerow(row)
input.close()
output.close()
This method takes in a item_num that is entered by the user, and deletes it from the list of dictionaries I have.此方法接受用户输入的 item_num,并将其从我拥有的字典列表中删除。 I am also trying to get this specific row deleted from my csv file that the data is stored in. For example, if the user inputs they would like to delete the row that has the Item # of 1, it would delete this row in the csv file.
我还试图从存储数据的 csv 文件中删除此特定行。例如,如果用户输入他们想要删除项目编号为 1 的行,它会在csv 文件。
I have the following method that I believe does so, but understand I cannot read and write to the same csv file at the same time, as it will simply clear it.我相信以下方法可以做到这一点,但请理解我无法同时读取和写入同一个 csv 文件,因为它只会清除它。 I know this isnt extremely practical, as it can load up memory, but I need to adjust the same csv file, and know this can be done by reading, closing it, and then writing.
我知道这不是很实用,因为它可以加载 memory,但我需要调整相同的 csv 文件,并且知道这可以通过读取、关闭然后写入来完成。 How would I do this?
我该怎么做?
The same CSV file should look like this after it is done if item_num = 2
.如果
item_num = 2
,相同的 CSV 文件在完成后应该如下所示。
Item #![]() |
Price![]() |
Quantity![]() |
Name![]() |
---|---|---|---|
1 ![]() |
2.99 ![]() |
1 ![]() |
Muffin 1![]() |
3 ![]() |
4.99 ![]() |
3 ![]() |
Cookie 1![]() |
4 ![]() |
5.99 ![]() |
4 ![]() |
Cookie 2![]() |
As you said, you can't (or really shouldn't try to ) concurrently read and write the same file.正如你所说,你不能(或者真的不应该尝试)同时读写同一个文件。 There are several options.
有几种选择。
<filename>_orig
<filename>_orig
mods
)mods
的新文件夹中) You're not doing it concurrently like the other user suggests.您不会像其他用户建议的那样同时执行此操作。 You're working with it sequentially, row by row, byte by byte.
您正在按顺序、逐行、逐字节地使用它。 It's completely safe.
这是完全安全的。 You may even use only a single descriptor doing it.
您甚至可以只使用一个描述符来执行此操作。 That should do it
应该这样做
def delete_item(self, item_num):
self.result[:] = [d for d in self.result if d.get("Item #") != int(item_num)]
editing = open('items2.csv', 'r+b')
pointer = 0
for row in csv.reader(editing):
if row[0] == item_num:
rest = editing.read()
editing.seek(pointer)
editing.write(rest)
editing.truncate()
editing.close()
break
pointer = editing.tell()
This code assumes there's only one row with the provided item #此代码假定提供的项目只有一行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.