Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending some rows in a .csv file from another .csv file 1

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
Hi,
I need to append some of the rows in a .csv file from another file. I have never done it before, I found some snippets online to do that.
I thought I should do it line by line.
First, I wanted to find lines for appending in a File1, then find the same in a File2 and use the last element of File2 to append to the specified lines in file1.
I stuck on the “find lines in File1/2. I need to match:
row[0] and row[1] File1 to :
row[0] and row[1] File2

Rows in File1 look like this:
match1, match2,something else,

Rows in File2 looking like this:
match1,match2,need this part for appending to File1,

Python:
from csv import writer
from csv import reader
match1 = 'ABBV50001'
match2 = 'CELL'
 
with open('C:/scripts/CSV/1.csv', 'r') as read_f1,         open('C:/scripts/CSV/2Ping.csv', 'r') as read_f2 :
 
    csv_reader1 = reader(read_f1)
    csv_reader2 = reader(read_f2)
 
 
    for row in csv_reader1 :
        if row[0] == match1 and row[1] == match2 :
            print (row)
         
        for row in csv_reader1 :
            if row[0] == match1 and row[1] == match2 :
                print (row)
 
OK. I could not make csv modules to work and I decided to go the long way.
I wanted to open both files, find matching lines, and append the last element of one file to the end of the second file.
For some reason, the code stops after the first match.
Here is a code:
Python:
import os
import re
 
 
hnd_stat = 'C:/Scripts/file1.txt'
cur_stat = 'C:/Scripts/file2.txt'
 
 
with open (cur_stat,'r+') as cur, open (hnd_stat, 'r') as hnd:
         
    for cln in cur :
        match_cnl = re.findall(r"^[A-Z]{4}\d{5},CELL", cln)
 
        if match_cnl :
            cln_sp = cln.split(",")
            cln_m  = cln_sp[0]+","+cln_sp[1]
 
            for hln in hnd :
                hln_sp = hln.split(",")
                hln_m = hln_sp[0]+","+hln_sp[1]
                     
                if hln_m in  cln :
                    print ("Appending ",cln+","+hln_sp[2])
 
Tester_V said:
For some reason, the code stops after the first match.
Because your file
Code:
open (hnd_stat, 'r') as hnd
is after the iteration loop
Code:
for hln in hnd :
  hln_sp = hln.split(",")
  ...
at end.
If you want to read from it the lines again, you have to open it again.
 
Modify your program like this:
Code:
with open (cur_stat,'r+') as cur:        

  for cln in cur :
    match_cnl = re.findall(r"^[A-Z]{4}\d{5},CELL", cln)

    if match_cnl :
      cln_sp = cln.split(",")
      cln_m  = cln_sp[0]+","+cln_sp[1]

      with open(hnd_stat, 'r') as hnd:        

        for hln in hnd:
          hln_sp = hln.split(",")
          hln_m = hln_sp[0]+","+hln_sp[1]
          if hln_m in  cln :
            print ("Appending ",cln.strip() + "," + hln_sp[2].strip())
            break
It should now work
 
In the previous code, the hnd-file have to be opened multiple times - i.e. for every line of cur-file. To avoid this, you can read at the beginning all lines from it into a list and then iterate multiple times over the list - like this:
Code:
with open (cur_stat,'r+') as cur, open(hnd_stat, 'r') as hnd:
  # read lines from hnd into list
  hnd_list_of_lines = hnd.readlines()

  for cln in cur :
    match_cnl = re.findall(r"^[A-Z]{4}\d{5},CELL", cln)

    if match_cnl :
      cln_sp = cln.split(",")
      cln_m  = cln_sp[0]+","+cln_sp[1]

      for hln in hnd_list_of_lines:
        hln_sp = hln.split(",")
        hln_m = hln_sp[0]+","+hln_sp[1]
        if hln_m in  cln :
          print ("Appending ",cln.strip() + "," + hln_sp[2].strip())
          break
 
Because i like dictionaries and this is a good example of where to use a 2D-dictionary, here is the example you can try:

Code:
hnd_stat = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]file1.txt[/color][COLOR=#ff00ff]'[/color]
cur_stat = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]file2.txt[/color][COLOR=#ff00ff]'[/color]

[COLOR=#0000ff]# read first file into 2D dictionary[/color]
match = [COLOR=#008b8b]dict[/color]()
[COLOR=#a52a2a][b]with[/b][/color] [COLOR=#008b8b]open[/color](hnd_stat, [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]r[/color][COLOR=#ff00ff]'[/color]) [COLOR=#a52a2a][b]as[/b][/color] hnd:
  [COLOR=#a52a2a][b]for[/b][/color] line [COLOR=#a52a2a][b]in[/b][/color] hnd:
    rows = line.strip().split([COLOR=#ff00ff]"[/color][COLOR=#ff00ff],[/color][COLOR=#ff00ff]"[/color])
    d = [COLOR=#008b8b]dict[/color]()
    d[rows[[COLOR=#ff00ff]1[/color]]] = rows[[COLOR=#ff00ff]2[/color]]
    match[rows[[COLOR=#ff00ff]0[/color]]] = d

[COLOR=#0000ff]# read second file, appending dictionary values to the lines [/color]
[COLOR=#a52a2a][b]with[/b][/color] [COLOR=#008b8b]open[/color] (cur_stat,[COLOR=#ff00ff]'[/color][COLOR=#ff00ff]r+[/color][COLOR=#ff00ff]'[/color]) [COLOR=#a52a2a][b]as[/b][/color] cur:        
  [COLOR=#a52a2a][b]for[/b][/color] line [COLOR=#a52a2a][b]in[/b][/color] cur :
    line = line.strip()
    [COLOR=#a52a2a][b]if[/b][/color] line:
      rows = line.split([COLOR=#ff00ff]"[/color][COLOR=#ff00ff],[/color][COLOR=#ff00ff]"[/color])
      [COLOR=#a52a2a][b]if[/b][/color] match[rows[[COLOR=#ff00ff]0[/color]]][rows[[COLOR=#ff00ff]1[/color]]]:
        [COLOR=#008b8b]print[/color] ([COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Appending [/color][COLOR=#ff00ff]"[/color], line + [COLOR=#ff00ff]"[/color][COLOR=#ff00ff],[/color][COLOR=#ff00ff]"[/color] + match[rows[[COLOR=#ff00ff]0[/color]]][rows[[COLOR=#ff00ff]1[/color]]])
 
Thank you all! Great snippets!
I'm ok now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top