0

I am comparing 2 csv files and trying to update the delimited line of one of the files if a criteria is met.

In this case i compare the first field of the first file with the 26th field of the second file, if the same value is found i want to update the very end of the string with an "OK" string. No error is shown but the line of the file is not updated, i suppose in the following case i am just updating the var instead of the actual file line but i don't know how to do that.

The simplified code is as follows:

import pandas as pd
import csv

file1 = 'C:\\Users\\MyName\\Desktop\\Some_Directory\\file1.csv'
file2 = 'C:\\Users\\MyName\\Desktop\\Some_Directory\\file2.csv'


for line in file1:

    fields_f1 = line.split(";")
    f1_field1 = fields_f1[0];

    for line2 in file2:
        fields_f2 = line2.split(",")
        f2_field26 = fields_f1[25]

        if f1_field1 is f2_field26:
            UpdateString = ";OK"
            line = str(line) + UpdateString
  • 1
    You need to open the file. I'm pretty sure you're just looping through the string's characters here. Print your lines out to help debug. – Daniel Abercrombie Jan 09 '18 at 19:28
  • I am actually looping trough their lines since it gives me the exactly field i want. I guess i could use some flags and open a third file to save the output exclusively. – Otorrinolaringologista -man Jan 09 '18 at 19:36
  • 1
    I found [this answer](https://stackoverflow.com/questions/1325905/inserting-line-at-specified-position-of-a-text-file/1325927#1325927) after a little searching. Maybe it will help. – Daniel Abercrombie Jan 09 '18 at 19:59

2 Answers2

0

A simple way to get this to work would be writing to a new .csv as you iterate over the first. though, it does look like you're intending to use pandas so why not load it into a dataframe, modify it within the frame and save it back to the file.

0

I've always been told not to update a file I'm reading/comparing.

When I'm doing something like comparing csv files like this I like to build a new list of file contents then check to see if the desired row or value is in the new list.

In your case I would;

  • Create a new list containing the csv file values your looking at
  • Define the string your looking for
  • Check that the desired string is in the 25th element of the new list
  • If it is append the 25th element with your desired string.
  • Write a new updated csv file with the list you built/appended

This is what I came up with using a single file and a predefined keyword to find.

For your case, just add the file you want to get your keyword from, then set the keyword to be the [0] element in that list/file

Hope this helps

import csv
import os


csv_file = os.path.join(os.path.join(os.environ['USERPROFILE']),'Dir', 'file.csv')
updated_csv_file_path = os.path.join(os.path.join(os.environ['USERPROFILE']),'Dir', 'updated_file.csv')

with open(csv_file, 'r') as rawcsv, open(updated_csv_file_path, 'w', newline='') as csv_out_file:
    reader = csv.reader(rawcsv)
    writer = csv.writer(csv_out_file)
    desired_file1_string = 'Greg'
    update_string = 'OK'
    file2_contents = []

    # adds all of rows (lists) found in the csv file to a new list for us to update
    for item in reader:
        file2_contents.append(item)
    print('Inital value of the 25th element: \n {}'.format(file2_contents[25]))
    # if the desired string is in the 25th element of the list, appends ';OK' to the end of that list
    if desired_file1_string in file2_contents[25]:
        file2_contents[25].append(update_string)
    print('Post append value of the 25th element: \n{}'.format(file2_contents[25]))

# writes a new csv file with the contents of file2_contents list
    writer = csv.writer(csv_out_file)
    for line in file2_contents:
        writer.writerow(line)

The results

Inital value of the 25th element:

['Greg', 'Unruh', '', 'some_words', 'some more words']

Post append value of the 25th element:

['Greg', 'Unruh', '', 'some_words', 'some more words', 'OK']