1

I am trying to calculate the tax of a numerical value extracted with xpath, to save it in a csv file, but I do not know what I am doing wrong or if I need to add something else.

This the code:

         import math
         for ntp in response.css('div.content-1col-nobox'):
             price1 = ntp.xpath('normalize-space(//tr[2]/td[@id="right_cell"][1])').extract()[0].split(None,1)[0].replace(",",".")
             tax = price1 * 0.22
             price = price1 + tax

          writer.writerow({
             '*StartPrice': price,\

and this is the error that I get:

    tax = price1 * 0.22
    TypeError: can't multiply sequence by non-int of type 'float'

Any help will be appreciate it.

Valter
  • 27
  • 8
  • Can you include a sample dataset so the error gets reproducible? – Ivo Apr 09 '18 at 14:47
  • I am trying to paste the whole code, but I don't know where – Valter Apr 09 '18 at 14:52
  • Sometimes, creating a sample data set is easier; see here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Ivo Apr 09 '18 at 14:53

1 Answers1

0

Lacking your data, this would be my suggestion:

import math
list_prices = ['11.15', '15.00', '1.1111']
taxed_prices = []
for price1 in list_prices:
    taxed = float(price1) * 1.22 # note that "* 1.22" saves you one step vis-a-vis "price *0.22 + price"
    taxed_prices.append(taxed)
print(taxed_prices)
Ivo
  • 3,890
  • 5
  • 22
  • 53
  • The price it's scraped by scrapy, extracted with xpath, basically is what you see in the original post, but before to pront the price in the csv file, I have to add the price with the tax included – Valter Apr 09 '18 at 14:55
  • If the response helps, click on "upvote" and "accept". ;) – Ivo Apr 09 '18 at 15:06
  • I get this error: `taxed = float(price1) * 1.22 ValueError: could not convert string to float: '.'` this is how I implemented: `listino = ntp.xpath('normalize-space(//tr[2]/td[@id="right_cell"][1])').extract()[0].split(None,1)[0].replace(",",".") taxed_prices = [] for price1 in listino: taxed = float(price1) * 1.22 taxed_prices.append(taxed) ` – Valter Apr 09 '18 at 15:13
  • have a look here: https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python – Ivo Apr 09 '18 at 15:18