I am new to Python, and programming in general. While I think this question may be related to my use of exception handling, it might also be due to a general lack of understanding!
for i in range(0, len(dates)):
try:
data.append(WUF.getwx(location[j], dates[i])[1])
continue
except xml.etree.ElementTree.ParseError:
#copy last good row of data and use it for the missing day
fixdata = data[-1] #[1,2,3,4,5,6,7,8,9,10,11]
fixdata[10] = 'estimated'
data.append(fixdata)
When I run the code as written, I get 2 “estimated” lines in data. One for the previous date, and one for the date that is being estimated. If I change the fixdata variable to [1, 2, 3, 4, 5, ...], then only one line (the intended line for the estimated date) is “estimated”.
Any idea what I’m missing here? Thanks!
The issue is in the line:
That doesn’t actually copy the data, it only copies the reference to the data.
fixdataends up pointing to the original element in the list, so when you then doit changes the original data.
To actually copy the data, try this:
[:]copies the whole list, which is what I think you’re trying to do.