Python Forum
Appending some rows in a .csv file from another .csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending some rows in a .csv file from another .csv file
#1
Hi,
I need to append some of the rows in a .csv file from another file. I have never did 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 Confused 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,

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)
Reply
#2
OK. I could not make csv models to work Wall 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 Confused .
Here is a code:
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])
Could anybody point me how to get around it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 760 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 921 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,397 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 973 Dec-13-2022, 03:19 PM
Last Post: finndude
  Appending a row of data in an MS Excel file azizrasul 3 1,171 Nov-06-2022, 05:17 PM
Last Post: azizrasul
Photo Making Zip file of a file and Directory Nasir 2 1,016 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,540 Sep-13-2022, 01:31 PM
Last Post: ManuRaval

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020