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

Unique variable declaration

I have a piece of code where I fill variables with data
and would like to add the first column of each row into the variable:
So each variable in a row should contain: %VAR_ + 'value of column[0]' + headerRow[i] + %
Now it looks like this which makes variables only unique within a row.

       result = []
        for line in lines:            
            i = 0
            for cell in row:
                tmp = ('%VAR_' + headerRow[i] + '%')
                line = line.replace(tmp, cell) # string.replace(old, new)
                i = i + 1
            result.append(line)
        return result
2
Contributors
4
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
5
Views
biscayne
Light Poster
36 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You should first test that what you want is there.

for cell in row:
    tmp = ('%VAR_' + headerRow[i] + '%')
    if tmp in line:
        line = line.replace(tmp, cell) # string.replace(old, new)
        print "replaced line =", line
    else:
        print tmp, "not found"
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9

What I want is there, but tmp (variable with column name included) is only unique per row, I want it to be unique for the whole file so it should contain some kind of row id.

biscayne
Light Poster
36 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Note that you will replace tmp with cell no matter when it is found, i.e the for loop will always replace tmp with cell 1 on the first pass if it is found, no matter where in the line it occurs. Without more info about what input and output should be, and what row contains, there is nothing more that can be said.

woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9

Thanks Woooee, got your point.
While debugging the program I can actually see what happens and that is rather
surprising ! ;-)
Thanks again.

biscayne
Light Poster
36 posts since Nov 2012
Reputation Points: 0
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.0764 seconds using 2.73MB