开发者

Getting Blank Lists - No Idea Why?

开发者 https://www.devze.com 2023-04-03 19:59 出处:网络
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\")

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:

 [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [开发者_Python百科], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
 [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消