read and write on same csv file

前端 未结 5 1131
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 11:00

I am trying to read and write on the same CSV file:

file1 = open(file.csv, \'rb\')
file2 = open(file.csv, \'wb\')
read         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 11:42

    This code could work:

    import csv
    with open(file,"r+") as file:
        break
    

    Note: I found that 'write' was affected since the previous value wasn't deleted before adding the next value so it would look something like this(writing='Hello'):

    >>>['Hello']

    >>>['HelloHello']

    I would suggest this:

    def writing(file,'what to write'):  
        with open(file, "w") as wfile:
            break
    
    def reading(file):
         with open(file, "r") as rfile:
            break
    

提交回复
热议问题