Question

I use this code:

from __future__ import division
from __future__ import print_function

in_file = open("s:/Personal Folders/Andy/Python Projects/People Cancelled/Analyze Authorize Truncated.csv")
text = in_file.readlines()
in_file.close()

header = text[0:1]
text = text[1:]

for index, line in enumerate(text):
    text[index] = line.split(",")

name = text
dates = text

for index, line in enumerate(name):
    name[index] = line[3:5]

for stuff, area in enumerate(dates):
     dates[stuff] = area[7:8]

print(name)
print(dates)

And I get this output:

 [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
 [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

Any idea why? For some reason there seems to be some sort of interference between the two of the for loops - if I do either individually I get the results I want.

Thanks!

Was it helpful?

Solution

Look at these two lines:

name = text
date = text

These lines do not make copies of text for name and date to point to; rather, they set both name and date to be in essence alternate names for text. So, after the third for loop (with body name[index] = line[3:5], every line in text is two items long (because text and name are the same list). In fact, I don't even see why you set both name and date equal to text initially; the intended results could be achieved simply by making name and date equal to new, empty lists and appending items to them based on text.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top