473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

search and replace with line in document

I am trying to write a code that searches and replaces with a line in the document. i have:

import re
x = open(r'F:\1\xxx.txt')
string = open(r'F:\1\xxx.txt').read()
Lines = x.readlines()
new_str = re.sub('zzz1', (Lines[0]) , string)

open(r'F:\1\xxx2.txt', 'w').write(new_str)

in xxx1 document i have written for testing:
1
2
3
4
5
6
7
8
9
10
zzz1
11

but the output I get in xxx2 document is:
1
2
3
4
5
6
7
8
9
10
1

11

can i have zzz1 replaced without the empty line to follow?
and how can i do it again say if wanting to replace a zzz2 with line 2?
Oct 14 '17 #1

✓ answered by chickflick91

working solution:

import re
x = open(r'F:\1\xxx.txt')
Lines = x.readlines()


repldict = {'zzz1':(Lines[0]).strip(), 'zzz2':(Lines[1]).strip()}
def replfunc(match):
return repldict[match.group(0)]

regex = re.compile('|'.join(re.escape(x) for x in repldict))
with open(r'F:\1\xxx.txt') as fin, open(r'F:\1\xxx2.txt','w') as fout:
for line in fin:
fout.write(regex.sub(replfunc,line))

2 1750
dwblas
626 Expert 512MB
See string replace at http://www.tutorialspoint.com/python/string_replace.htm Also take a look a the files section in the same tutorial on how to use newlines when writing a file.
Oct 14 '17 #2
working solution:

import re
x = open(r'F:\1\xxx.txt')
Lines = x.readlines()


repldict = {'zzz1':(Lines[0]).strip(), 'zzz2':(Lines[1]).strip()}
def replfunc(match):
return repldict[match.group(0)]

regex = re.compile('|'.join(re.escape(x) for x in repldict))
with open(r'F:\1\xxx.txt') as fin, open(r'F:\1\xxx2.txt','w') as fout:
for line in fin:
fout.write(regex.sub(replfunc,line))
Oct 15 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
0
by: DaveJG | last post by:
I have a couple of questions involving the search/replace in the IDE and regular expressions. Any help would be greatly appreciated. 1. How would I replace two line feeds with one? That is ...
23
by: SeaPlusPlus | last post by:
I want to convert large files of prose to xhtml and so I need a way to remove unwanted line wraps. So, I'm looking for a freebee editor that has the capability of searching for a single "carriage...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
2
by: Michael Peters | last post by:
is there a way to replace a certain sequence of characters by line feed (vbCrLf ), for all text columns in a table, using Search+Replace? -Michael
5
by: DarthDaddy | last post by:
I hope to explain this properly. Here is a sample section of a file I am working with: <achievements> <achievement> <item name="COURSE_COMPLETION_DATE">19930630</item> <item...
1
by: laredotornado | last post by:
Hello, What am I doing wrong in my regular expression? I'm trying to replace line break characters by responseText = responseText.replace(/(\r|\n)/, ""); However my response still...
1
by: Sandeep Akhare | last post by:
Hi All i have given a problem set in which i need to parse/load/search a xml document of near about 1 GB in dot net. Please help me in this also do not have the luxury to dump the data into a...
3
by: Compufix | last post by:
I am trying to edit a file in place.....it is the /etc/master.passwd in OSX I need to push out a script that adds a crypt password for root. The file by default has an * so the line starts with ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.