简体   繁体   English

同时读取和写入CSV文件

[英]Reading and Writing into CSV file at the same time

I wanted to read some input from the csv file and then modify the input and replace it with the new value. 我想从csv文件中读取一些输入,然后修改输入并将其替换为新值。 For this purpose, I first read the value but then I'm stuck at this point as I want to modify all the values present in the file. 为此,我首先读取了该值,但是由于要修改文件中存在的所有值,因此目前处于卡住状态。 So is it possible to open the file in r mode in one for loop and then immediately in w mode in another loop to enter the modified data? 那么有可能在一个for循环中以r模式打开文件,然后在另一个循环中立即以w模式打开文件以输入修改后的数据吗?

If there is a simpler way to do this please help me out 如果有更简单的方法,请帮帮我

Thank you. 谢谢。

您可以执行open("data.csv", "rw") ,这使您可以同时进行读写。

Yes, you can open the same file in different modes in the same program. 是的,您可以在同一程序中以不同的模式打开同一文件。 Just be sure not to do it at the same time. 只是要确保不要同时进行。 For example, this is perfectly valid: 例如,这是完全正确的:

with open("data.csv") as f:
  # read data into a data structure (list, dictionary, etc.)
  # process lines here if you can do it line by line

# process data here as needed (replacing your values etc.)

# now open the same filename again for writing
# the main thing is that the file has been previously closed
# (after the previous `with` block finishes, python will auto close the file)
with open("data.csv", "w") as f:
  # write to f here

As others have pointed out in the comments, reading and writing on the same file handle at the same time is generally a bad idea and won't work as you expect (unless for some very specific use case). 正如其他人在评论中指出的那样,同时读取和写入同一文件句柄通常是一个坏主意,并且不会按您期望的那样工作(除非对于某些非常特殊的用例)。

Just like others have mentioned, modifying the same file as both input and output without any backup method is such a terrible idea, especially in a condensed file like most .csv files, which is normally more complicated than a single .Txt based file, but if you insisted you can do with the following: 就像其他人提到的那样,在没有任何备份方法的情况下将相同文件修改为输入和输出是一个很糟糕的主意,尤其是在像大多数.csv文件这样的压缩文件中,该文件通常比单个基于.Txt的文件复杂,但是如果您坚持可以执行以下操作:

import csv

file path = 'some.csv'

with open('some.csv', 'rw', newline='') as csvfile:
    read_file = csv.reader(csvfile)
    write_file = csv.writer(csvfile)

Note that code above will trigger an error with a message ValueError: must have exactly one of create/read/write/append mode . 请注意,上面的代码将触发错误,并显示一条消息ValueError: must have exactly one of create/read/write/append mode

For safety, I preferred to split it into two different files 为了安全起见,我宁愿将其分成两个不同的文件

import csv

in_path = 'some.csv'
out_path = 'Out.csv'

with open(in_path, 'r', newline='') as inputFile, open(out_path, 'w', newline='') as writerFile:
    read_file = csv.reader(inputFile)
    write_file = csv.writer(writerFile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

    for row in read_file:
    # your modifying input data code here
    ........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM