Replacing a line in a file based on a keyword search, by line from another file

2024/10/12 10:18:33

Here is my file1:

agadfad
sdffasdf
Element 1, 0, 0, 0
Pcom
Element 2

Here is my file2:

PBAR
Element 1, 100, 200, 300, 400
Element 2
Continue...

I want to search with a keyword, "Element 1" in file1, if found store the whole line; then search in file2, if found at some line, replace it with the data from file1 which is in this case "Element 1,0,0,0". Similarly, if there are more keywords like "Element 2, Element 3 and so on...", and the files are very big, it should do the same (But this part comes later). I tried following code:

    index1 = 0index2 = 0path1 = "C:\Users\sony\Desktop\BDF1.TXT"path2 = "C:\Users\sony\Desktop\BDF2.TXT"Target = 'Element 1'with open(path1) as f1:list1 = f1.readlines()for line in list1:index1 = index1 + 1if Target in line:print "Match Found at line %d" %(index)else:print "No Match Found in the target file!"with open(path2, "r+") as f2:list2 = f2.readlines()for line2 in list2:index2 = index2 + 1if Target in line2:list2[index2] = line + '                    \n'else:print "No match found in the targetorg file!"f2.writelines(list2)

I am getting some output which looks like this:

PBAR
Element 1, 100, 200, 300, 400
Element 2
Continue... PBAR
Element 1, 100, 200, 300, 400
agadfad
Continue...

And i am also getting error list assignment index out of range at somewhere line 20. It seems easier, but having hard time to figure it out.

Answer

Regular expressions will do what you want easily, assuming the format is always the same. That is each line has the format "Element N, more stuff", where

  • "Element N" is always capitalised, followed by a space and then only numbers
  • more stuff is consisted of only spaces, commas and numbers.

Code

import rewith open(path1) as f1, open(path2) as f2:dat1 = f1.read()dat2 = f2.read()matches = re.findall('^Element [0-9]+,[0-9, ]+', dat1, flags=re.MULTILINE)for match in matches:dat2 = re.sub('^{},[0-9, ]+'.format(match.split(',')[0]), match, dat2, flags=re.MULTILINE)with open('changed.txt', 'w') as f:f.write(dat2)

Explanation

The pattern "^Element [0-9]+,[0-9, ]+" starts at the beginning of a line (because of ^), and matches the string Element, followed by a space, followed by any length of numbers ([0-9]+), followed by a comma, followed by any length of a combination of numbers, commas and spaces ([0-9, ]+). The will effectively find "Element 1, 0, 0, 0", "Element 2, 123, 123, 123, 123, 123" (for example), etc.

Then you iterate through these matches, search for a string that matches "Element 1,[0-9, ]+" (and so on) in the second file and substitute it for the match from the first file.

https://en.xdnf.cn/q/118213.html

Related Q&A

How to check for pop up alert using selenium in python

What I want is to continue with the next iteration if there is a pop up message in the webpage being scrapped. That is if there is any pop up message, I want to accept that message and go to the next i…

Rally host is non-existent or unreachable via pyral

I am trying to call rally server simply using below: rally = Rally(server, user, password, workspace=workspace, project=project)But it is giving below error:Traceback (most recent call last):File "…

Query tangled array in Pymongo

I am trying to query a very tangled collection. The schema:{tags: {variables: [{value: 3x9, var_name: s},{value: 12:00AM, var_name: x},{value: goog, var_name: y}]},url: https://www.google.com}]The Quer…

manipulating value of pandas dataframe cell based on value in previous row without iteration

I have a pandas dataframe with~3900 rows and 6 columns compiled from Google Finance . One of these columns defines a time in unix format, specifically defining a time during the trading day for a marke…

Convert ctypes code to cython

Id like to convert some ctypes code to use cython instead, but Im struggling. Essentially, the ctypes code:copies the contents (floats) of two lists into C-compatible structs sends the structs to my b…

Enable PyROOT Ubuntu 14.04

I downloaded madpgraph5, but when I run it I get the following error:ERROR: ROOT file called ROOT.py or ROOT.pyc is not foundERROR: Please check that ROOT is properly installed.When I try locate ROOT.p…

pygal on windows - cannot access classes from pygal

I have such short script:import pygal if __name__ == __main__:bar_chart = pygal.Bar()and following error: AttributeError: module object has no attribute BarDo you have any idea what is wrong? Shall I …

Parsing table for a link

Ive been able to isolate a row in a html table using Beautiful Soup in Python 2.7. Been a learning experience, but happy to get that far. Unfortunately Im a bit stuck on this next bit.I need to get t…

build matrix from blocks

I have an object which is described by two quantities, A and B (in real case they can be more than two). Objects are correlated depending on the value of A and B. In particular I know the correlation m…

How to convert string with UTC offset

I have date as In [1]: a = "Sun 10 May 2015 13:34:36 -0700"When I try to convert it using strptime, its giving error.In [3]: datetime.strptime(a, "%a %d %b %Y %H:%M:%S %Z"...: ) ---…