Home > front end >  removing duplicate id entry from text file using python
removing duplicate id entry from text file using python

Time:12-15

I have a text file which contains this data (items corresponds to code,entry1,entry2) :

a,1,2
b,2,3
c,4,5
....
....

Here a,b,c.. will be unique always

Every time I read this file in python to either create a new entry for example d,6,7 or to update existing values: say a,1,2 to a,4,3.

I use the following code :

data = ['a',5,6]
datastring = ''
        for d in data
            datastring = datastring   str(d)   ','
        try:
            with open("opfile.txt", "a") as f:
                f.write(datastring   '\n')
            f.close()
            return(True)
        except:
            return(False)

This appends any data as a new entry. I am trying something like this which checks the first character of each line:

f = open("opfile.txt", "r")
        for x in f:
            if(x[0] == username):
               pass

I don't know how to club these two so that a check will be done on first character(lets say it as id) and if an entry with id is already in the file, then it should be replaced with new data and all other data remains same else it will be entered as new line item.

CodePudding user response:

Read the file into a dictionary that uses the first field as keys. Update the appropriate dictionary, then write it back.

Use the csv module to parse and format the file.

import csv

data = ['a',5,6]

with open("opfile.txt", "r", newline='') as infile:
    incsv = csv.reader(infile)
    d = {row[0]: row for row in incsv if len(row) != 0}

d[data[0]] = data

with open("opfile.txt", "w") as outfile:
    outcsv = csv.writer(outfile)
    outcsv.writerows(d.values())

CodePudding user response:

first append all new row to the file.

second, try using write to update rows in your file

def update_record(file_name, field1, field2, field3):
    with open(file_name, 'r') as f:
        lines = f.readlines()
    with open(file_name, 'w') as f:
        for line in lines:
            if field1 in line:
                f.write(field1   ','   field2   ','   field3   '\n')
            else:
                f.write(line)
  • Related