We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,991 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Python -replace from array problem

Hello everybody,

I wrote a script as like below

arr = [(line.rstrip('\n').split(';')) for line in open('C:/Config_Changer.csv')]
import sys
import fileinput
for i, line in enumerate(fileinput.input('C:/1.cfg', inplace = 1)):
    for f,t in arr:
        if 'description' not in line:
              line = line.replace(f, t)
    sys.stdout.write(line)

ok my problem is my config changer file is like
1/1/1;1/1/2
1/1/2:1/1/4

when I run the script it first change 1/1/1 to 1/1/2 then to 1/1/4 but I don't want that; I want that it changes 1/1/1 to 1/1/2 and 1/1/2 to 1/1/4 so when its replacing the values from array;I want that it should read from the first buffered file not the last changed file :) How can I do that ?

Thank you very much for all your help in advance

2
Contributors
6
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
7
Views
alpdoruk
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Here's your problem -

for f,t in arr:
        if 'description' not in line:
              line = line.replace(f, t)

It will keep changing that line, for each value of f and t that you have in arr.
You need to break out of the for loop, or do something so that it only changes the line once.

Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
Skill Endorsements: 0

yes I know but I can't break the loop.I need that it changes all the array.
What else can I do for changing a line once ?

alpdoruk
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

yes I know but I can't break the loop.I need that it changes all the array.
What else can I do for changing a line once ?

This doesn't make sense. Let's say you have a file with the line

1/1/1

And your arr values were [ (1/1/1, 1/1/2), (1/1/2, 1/1/3), (1/1/3, 1/1/4) ]

Going through the for loop -

Line -
1/1/1
Replace line, line is now 1/1/2
Replace line, line is now 1/1/3
Replace line, line is now 1/1/4


This isn't what you want, so if you had instead

Line -
1/1/1
Replace line, line is now 1/1/2, break

You wouldn't be having issues.

Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
Skill Endorsements: 0

ok but afterwards
how can I change 1/1/2 to 1/1/3 if I break after changing 1/1/1 to 1/1/2 ?

alpdoruk
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

ok but afterwards
how can I change 1/1/2 to 1/1/3 if I break after changing 1/1/1 to 1/1/2 ?

For line in file:
    For f, t in arr:
        If line matches current value of f, replace and break

Nothing fancy, just try to think about what your for loops are doing.

Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13
Skill Endorsements: 0

I am not able to change loop correctly but with that break at least it's replacing one line correct :)

alpdoruk
Newbie Poster
4 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0699 seconds using 2.69MB