I am trying to write data to an excel file. Every link that meets the requirements in the if-test should be written out in the excel file. It starts writing at (0,0) and goes on downwards in the same column (0,1),(0,2).. (0,3) etc. The problem is that it writes out data to the excel file, but only when the if-test has reached its last time.
Json-file:
[
{
"beds": "3",
"bath": "2",
"link": "https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790",
"price": "382,76"
},
{
"beds": "3",
"bath": "1",
"link": "https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790",
"price": "382,76"
},
{
"beds": "2",
"bath": "3",
"link": "https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790",
"price": "382,76"
},
{
"beds": "3",
"bath": "2",
"link": "https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790",
"price": "382,76"
}
]
Python code: Tried this
import json
import re
from xlwt import Workbook
class Products:
def __init__(self):
self.list_links=[]
def product(self,index):
for k, v in index.items():
if k=='link':
link=v
if k=='bath':
bath=v
fl_bath=int(bath)
wb=Workbook()
sheet1=wb.add_sheet('sheet1')
sheet1.col(0).width = 7000
if fl_bath >= 2:
length=len(self.list_links)
sheet1.write(length,0,link)
self.list_links.append(link)
print(link)
wb.save("python.xls")
with open('./try.json') as json_file:
data = json.load(json_file)
i=0
p=Products()
while i <= 3:
dicts = data[i]
p.product(dicts)
i+=1
It should write out the links downwards in each row in the excel file, every links thats meets the requirments:
- row1: https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790
- row2: https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790
- row3: https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790
I get this output (excel-file):
- row1:
- row2:
- row3: https://www.realestate.com/5619-w-michelle-dr-glendale-az-85308--790
3 of the links meets the criterium. But only the last one in the iteration gets written out in the excel file. Are they being overwritten in some way after each iteration? Any good tips on how to fix this?