1

I have a python script that imports a CSV file and based on the file imported, I have a list of the indexes of the file.

I am trying to match the indexes in FILESTRUCT to the CSV file and then replace the data in the column with new generated data. Here is a code snip-it:

This is just a parsed CSV file returned from my fileParser method:

PARSED = fileParser() 

This is a list of CSV column positions:

FILESTRUCT = [6,7,8,9,47]

This is the script that is in question:

def deID(PARSED, FILESTRUCT):
    for item in PARSED:
            for idx, lis in enumerate(item):                        
                    if idx == FILESTRUCT[0]:
                           lis = dataGen.firstName()

                    elif idx == FILESTRUCT[1]:
                            lis = dataGen.lastName()

                    elif idx == FILESTRUCT[2]:
                            lis = dataGen.email()

                    elif idx == FILESTRUCT[3]:
                            lis = dataGen.empid()

                    elif idx == FILESTRUCT[4]:
                            lis = dataGen.ssnGen()

                    else:
                            continue

    return(PARSED)

I have verified that it is correctly matching the indices (idx) with the integers in FILESTRUCT by adding a print statement at the end of each if statement. That works perfectly.

The problem is that when I return(PARSED) it is not returning it with the new generated values, it is instead, returning the original PARSED input values. I assume that I am probably messing something up with how I use the enumerate method in my second loop, but I do not understand the enumerate method well enough to really know what I am messing up here.

1
  • please describe input and output, i..e a MCVE Commented Jan 13, 2016 at 21:10

3 Answers 3

1

You can use

item[idx] = dataGen.firstName()

to modify the underlying item. The reason here is that enumerate() returns (id, value) tuples rather than references to the iterable that you passed.

Given your example above you may not even need enumerate, because you're not parsing the lis at all. So you could also just do

for i in range(len(item)):
    # your if .. elif statements go here ...
    item[i] = dataGen.firstName()

On a side-note, the elif statements in your code will become unwieldy once you start adding more conditions and columns. Maybe consider making FILESTRUCT a dictionary like:

FILESTRUCT = {
    6: dataGen.firstName,
    7: dataGen.lastName,
    ....
    }
...
for idx in range(len(item)):
    if idx in FILESTRUCT.keys():
        item[idx] = FILESTRUCT[idx]()
Sign up to request clarification or add additional context in comments.

Comments

0

So PARSED is an iterable, and item is an element of it and is also an iterable, and you want to make changes to PARSED by changing elements of item.

So let's do a test.

a = [1, 2, 3]
print 'Before:'
print a

for i, e in enumerate(a):
    e += 10

print 'After:'
print a

for e in a:
    e += 10

print 'Again:'
print a

a[0] += 10

print 'Finally:'
print a

The results are:

Before:
[1, 2, 3]
After:
[1, 2, 3]
Again:
[1, 2, 3]
Finally:
[11, 2, 3]

And we see, a is not changed by changing the enumerated elements.

Comments

0

You aren't returning a changed variable. You don't ever change the variable FILESTRUCT. Rather make another variable, make it as you loop through FILESTRUCT and then return your new FILE.

You can't change the values in a loop like that, Kind of like expecting this to return all x's:

demo_data = "A string with some words"
for letter in demo_data:
    letter = "x"
return demo_data

It won't, it will return: "A string with some words"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.