LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 05-19-2009, 12:13 PM   #1
tonya11en
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 = ['zeta21000.nc','zeta21001.nc','zeta21002.nc','zeta21003.nc','zeta21004.nc','zeta21005.nc','zeta21006 .nc','zeta21007.nc','zeta21008.nc','zeta21009.nc','zeta21010.nc','zeta21011.nc','zeta21012.nc','zeta 21013.nc','zeta21014.nc','zeta21015.nc','zeta21016.nc','zeta21017.nc','zeta21018.nc','zeta21019.nc', 'zeta21020.nc','zeta21021.nc','zeta21022.nc','zeta21023.nc','zeta21024.nc','zeta21025.nc']

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])

o.close()

Last edited by tonya11en; 05-19-2009 at 12:16 PM.
 
Old 05-19-2009, 02:13 PM   #2
The_Kernel
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:

Code:
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')
out_file.write(lines) 
out_file.close()
 
Old 05-19-2009, 07:52 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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


Reply


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 05:20 PM
python: how to handle unicode chars in ascii strings? BrianK Programming 1 01-26-2009 10:46 PM
replacing strings in many files using tcsh mcbenus Linux - Software 5 03-03-2008 06:50 PM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 11:07 AM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 11:13 PM


All times are GMT -5. The time now is 10:16 PM.

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