yujx
yujx

Reputation: 11

Writing to excel file with python

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:

I get this output (excel-file):

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?

Upvotes: 0

Views: 125

Answers (3)

Prajyot Naik
Prajyot Naik

Reputation: 86

Problem here is that for every iteration you are creating a new workbook and a sheet and writing one link and saving it as "python.xls" every time. You should create the workbook outside the the function, only once and in the function product write a link to it. Something like this:

import json
from xlwt import Workbook

wb = Workbook()
sheet1 = wb.add_sheet('sheet1')
sheet1.col(0).width = 7000

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)

        if fl_bath >= 2:
            length=len(self.list_links)
            sheet1.write(length,0,link)
            self.list_links.append(link)
            print(link)

with open('./try.json') as json_file:  
    data = json.load(json_file)

while i <= 3:
    dicts = data[i]
    p.product(dicts)
    i+=1

wb.save("python.xls") 

Upvotes: 0

Paolo
Paolo

Reputation: 26034

You can simplify your code since the requirement is a simple greater-than comparison:

import json
from xlwt import Workbook

with open('inputFile.json') as json_file:
    data = json.load(json_file)

wb = Workbook()
firstSheet = wb.add_sheet('sheet1')
firstSheet.col(0).width = 7000
row = -1

for item in data:
    if int(item['bath']) >= 2:
        row = row + 1
        firstSheet.write(row,0,item['link'])

wb.save("outputFile.xls")

Upvotes: 2

crayxt
crayxt

Reputation: 2405

It seems like you are overwriting Excel file each time? Move your workbook definition code to the init of Product class and save function to separate class method and call it after processing of your dict.

Upvotes: 0

Related Questions