1,105,254 Community Members

Unique variable declaration

Member Avatar
biscayne
Light Poster
36 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
woooee
Posting Maven
2,793 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
1
 

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"
Member Avatar
biscayne
Light Poster
36 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
woooee
Posting Maven
2,793 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
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.

Member Avatar
biscayne
Light Poster
36 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: