473,396 Members | 1,849 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.

replace a set of strings in a .txt file

hi everyone,

My problem goes like this (*.d) which can be opened using wordpad.
Now I need to replace some strings inside my file which is enclosed within double quotes.
I have to use a I/P file as a text delimited file which have two columns first column old string and the second one is new string.
As Im new to python language can some one help me how to start with it..?

Thanks in advance.
Feb 7 '12 #1
1 1565
bvdet
2,851 Expert Mod 2GB
Create a dictionary from the file with 2 columns.
Expand|Select|Wrap|Line Numbers
  1. dd = {"Col 1 text 1": "Col 2 text 1", "Col 1 text 2": "Col 2 text 2"}
I might approach replacing the text like this:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> text = 'A line in text file containing "Col 1 text 1"'
  3. >>> dd = {"Col 1 text 1": "Col 2 text 1", "Col 1 text 2": "Col 2 text 2"}
  4. >>> patt = re.compile(r'"(.+)"')
  5. >>> m = patt.search(text)
  6. >>> if m and m.group(1) in dd:
  7. ...     text = text.replace(m.group(1), dd[m.group(1)])
  8. ...     
  9. >>> text
  10. 'A line in text file containing "Col 2 text 1"'
  11. >>> 
Feb 7 '12 #2

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

Similar topics

9
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © import sys © © nn = len(sys.argv) © © if not nn==5: © print "error: %s search_text replace_text in_file out_file" % sys.argv
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
1
by: Christian-Josef Schrattenthaler | last post by:
Hi! I have a batch-file (*.bat) which logon the client to our server, including printer and drive mappings. Now I want to replace the batch-file with a script for the windows scripting host,...
0
by: ianstratford | last post by:
I just released a new version of the free Microsoft Access find and replace add-in utility (version 1.5). Search and replace (optionally) any string in tables, queries, macros, forms, reports...
7
agsrinivasan
by: agsrinivasan | last post by:
hi everyone, i have a txt file..for eg... 1. WA.1048/2005 M/S.R.SIVAKUMAR M/S.PAUL AND PAUL S.SURESH ...
1
by: gjsetnes | last post by:
I have a lot of customers who have my Access.ADP application installed on their network. Each Access.ADP client application is installed at the workstation. I do this when I initially install the...
2
by: yxq | last post by:
Hello, I want to update A program file from A. First startup A, then startup B process from A, close A, B will copy new file to replace A file. But system states that A is using. A code:...
2
by: Eric Kaplan | last post by:
I have a function that will take char buffer and dump all data into a file. How can I replace all <givenname xsi:nil='true'/> with <givenname>.</givenname>
1
by: mukeshrasm | last post by:
Hi I want to delete or replace the existing file from directory using PHP. I don't want to rename the file.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.