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
#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


Messages In This Thread
RE: Appending some rows in a .csv file from another .csv file - by tester_V - Aug-26-2020, 01:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 913 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  file open "file not found error" shanoger 8 1,648 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 941 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 1,163 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,272 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,455 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,287 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 1,132 Dec-13-2022, 03:19 PM
Last Post: finndude
  Appending a row of data in an MS Excel file azizrasul 3 1,366 Nov-06-2022, 05:17 PM
Last Post: azizrasul
Photo Making Zip file of a file and Directory Nasir 2 1,161 Oct-07-2022, 02:01 PM
Last Post: Nasir

Forum Jump:

User Panel Messages

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