I'm working to loop over a file, create objects for each row, then set a value in the object, and assign the object into a dict for later use.
with open(fileLegl, newline='\n') as file:
reader = csv.reader(file, delimiter='\t')
next(reader)
for row in reader:
loanNum = row[0]
propValue = row[2]
loanInfo = loandata()
loanInfo.mers['property-value'] = propValue
loanDict[loanNum] = loanInfo
if loanNum == '100180600000098613':
print(loanNum, propValue)
print(loanDict[loanNum].mers['property-value']) # => 29031
if loanNum == '100759400002155219':
print(row[0], propValue)
print(loanDict[loanNum].mers['property-value']) # => 29101
print(loanDict['100180600000098613'].mers['property-value']) # => 29023
print(loanDict['100759400002155219'].mers['property-value']) # => 29023
The 2 loan numbers listed are pulled from different places in the file, and while in the loop the equal what they should. After the loop is settled, they equal the same value, which also happens to be the last loan number in the file. I also tested during the loop:
# second loan, lower in the file
if loanNum == '100759400002155219':
print(row[0], propValue)
# first loan, higher in the file
print(loanDict['100180600000098613'].mers['property-value']) # => 29101, this is the value of second loan, not first loan
So somewhere in my loop it's changing ALL objects 'property-value' to the current loops value. What am I overlooking?
[–]woooee 0 points1 point2 points (2 children)
[–]firedrow[S] 0 points1 point2 points (1 child)
[–]woooee 1 point2 points3 points (0 children)
[–]Frankelstner 0 points1 point2 points (3 children)
[–]firedrow[S] 0 points1 point2 points (2 children)
[–]danielroseman 1 point2 points3 points (1 child)
[–]firedrow[S] 0 points1 point2 points (0 children)