×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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,

CODE --> 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) 

RE: Appending some rows in a .csv file from another .csv file

(OP)
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:

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]) 

RE: Appending some rows in a .csv file from another .csv file

Quote (Tester_V)


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.

RE: Appending some rows in a .csv file from another .csv file

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

RE: Appending some rows in a .csv file from another .csv file

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 

RE: Appending some rows in a .csv file from another .csv file

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 = 'file1.txt'
cur_stat = 'file2.txt'

# read first file into 2D dictionary
match = dict()
with open(hnd_stat, 'r') as hnd:
  for line in hnd:
    rows = line.strip().split(",")
    d = dict()
    d[rows[1]] = rows[2]
    match[rows[0]] = d

# read second file, appending dictionary values to the lines 
with open (cur_stat,'r+') as cur:        
  for line in cur :
    line = line.strip()
    if line:
      rows = line.split(",")
      if match[rows[0]][rows[1]]:
        print ("Appending ", line + "," + match[rows[0]][rows[1]]) 

RE: Appending some rows in a .csv file from another .csv file

(OP)
Thank you all! Great snippets!
I'm ok now.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close