0

I am building a small tool in Python, the function of the tool is the following:

Open a master data file (cvs format) open a log file (cvs format) Request the user to select the pointer for the field that will need to be compared in both files. Start comparing record by record when the field is not found in the record proceed to look forward in the log file until the field can be found, in the meantime keep in memory the pointer for when the comparison will be continued. Once the field is found proceed to cut the whole record off that line and place it in the right position

Here is an example :

Data file "1","1234","abc" "2","5678","def" "3","9012","ghi"

log file "1","1234","abc" "3","9012","ghi" "2","5678","def"

final log file : "1","1234","abc" "2","5678","def" "3","9012","ghi"

I was looking in the cvs lib in python and the sqlite3 lib but there is nothing that seems to really do a swap in file so i was thinking that maybe i should just create a new file with all the records in order.

What could be done in this regard ? Is there a library or a command that can move records in an existing file ?

I would prefer to modify the existing file instead of creating a new one but if it's not possible i would just move to create a new one.

In addition to that the code i was planning to use to verify the files was this :

import csv
reader1 = csv.reader(open('data.csv', 'rb'), delimiter=',', quotechar='"'))
row1 = reader1.next()
reader2 = csv.reader(open('log.csv', 'rb'), delimiter=',', quotechar='"'))
row2 = reader2.next()
if (row1[0] == row2[0]) and (row1[2:] == row2[2:]):
    #here it move to the next record
else:
    #here it would run  a function that replace the field

Please note that this piece of code was found at this page :

Python: Comparing specific columns in two csv files

(i don't want to take away the glory from another coder).

I just like it for it's simplicity.

Thanks to all for the attention.

Regards

Danilo

matisetorm
  • 857
  • 8
  • 21
Danilo Patrucco
  • 110
  • 3
  • 10
  • Welcome to SO! Please only include information relevant to your question in your post and state your question as clearly and concisely as possible. Please review these [Question guidelines](https://stackoverflow.com/help/how-to-ask) – sgrg Apr 21 '17 at 16:14
  • Why not just replace data.csv with log.csv? Isn't that all your code is doing? – CamJohnson26 Apr 21 '17 at 17:18
  • the log and data do not necessarily need to be the same, they may be different, they will have in common at least one field. – Danilo Patrucco Apr 21 '17 at 20:35

0 Answers0