Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-19-2009, 11:13 AM   #1
LQ Newbie
Registered: May 2009
Posts: 1

Rep: Reputation: 0
Python: replacing strings in a file

I am trying to write a program that contains a loop that replaces file names in a text file with names from a set list that I have. It fails to run and I was hoping somebody could tell me why and/or figure out how to get it working.


import string, sys, os, re, fileinput

i = ['','','','','','','zeta21006 .nc','','','','','','','zeta','','','','','','', '','','','','','']

j =['zeta2Em007','4Em007','8_00Em007','1_60Em006','3_20Em006','6_40Em006','1_28Em005','2_56Em005','5_12E m005','1_02Em004','2_05Em004','4_10Em004','8_19Em004','1_64Em003','3_28Em003','6_55Em003','1_31Em002 ','2_62Em002','5_24Em002','1_05Em001','2_10Em001','4_19Em001','8_39Em001','1_68E000','3_36E000','6_7 1E000']

o = open("ensight_command","w")
data = open("ensight_command").read()

for x in range(0,26)
os.system("ensight82 -p ensight_command")
o.write( re.sub(i[x],i[x+1],data))
o.write( re.sub(j[x],j[x+1],data))
#ensightfile = ensightfile.replace(i[x], i[x+1])
#ensightfile = ensightfile.replace(j[x], j[x+1])


Last edited by tonya11en; 05-19-2009 at 11:16 AM.
Old 05-19-2009, 01:13 PM   #2
LQ Newbie
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
It would be easier to use a dictionary to store the search and replace terms. Also there's no need to write all the intermediate steps out to the file, just save them as string. As a generic solution here's what I'd do:

replace_dict = {'foo': 'bar',   
                'help': 'okay',
                'more': 'less'}
lines = open('test_file.txt').read() 
for search, replace in replace_dict.items():
    lines = lines.replace(search, replace)
out_file = open('test_file.txt', 'w')
Old 05-19-2009, 06:52 PM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Originally Posted by tonya11en View Post
o = open("ensight_command","w")
data = open("ensight_command").read()
use fileinput , since you already imported it.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
python: how to handle unicode chars in ascii strings? BrianK Programming 5 01-27-2009 04:20 PM
python: how to handle unicode chars in ascii strings? BrianK Programming 1 01-26-2009 09:46 PM
replacing strings in many files using tcsh mcbenus Linux - Software 5 03-03-2008 05:50 PM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration