Python Forum

Full Version: ValueError: could not convert string to float
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

i am trying to crawl a website which is

when i run the code i receive one error which is value error could not convert string to float, i need it's value to calculate some formula but after trying hard i didnt find any solution, can someone please help me on this?

import requests
from bs4 import BeautifulSoup
import sqlite3
import json
#import schdule
import csv
#from flask import Flask, render_template
final_data = []
url = ""
#app = Flask(__name__)
def dbCurs():
    con = sqlite3.connect("NSE.db")
    con.row_factory = sqlite3.Row
    cur = con.cursor()
    return con, cur
def closeDBCurs(con, cur):

def get_data():
    global final_data
    con, cur = dbCurs()
    r = requests.get(url)
    data = r.text
    dta = json.loads(data)
    lenght = dta["data"]
    sublist = []
    for i in range(0, len(lenght)):
        data = lenght[i]
        if data["open"]==data["low"]:
            name = data["symbol"]
            opne = data["open"]
            high = data["high"]
            low = data["low"]
            ltp = data["ltP"]
            stoploss = opne
            target = float(high) + (float(high) * 0.3/100)
            cur.execute("INSERT OR REPLACE INTO BUY VALUES(NULL, ?,?,?,?,?,?,?)",(name, opne, high, low,ltp, stoploss, target))
        if data["open"]==data["high"]:
            name = data["symbol"]
            opne = data["open"]
            highs = data["high"]
            low = data["low"]
            ltp = data["ltP"]
            stoploss = opne
            target = ""#high + high * 0.3/100 # need to create result here
            cur.execute("INSERT OR REPLACE INTO SELL VALUES(NULL, ?,?,?,?,?,?,?)",(name, opne, high, low, ltp, stoploss, target))
    return final_data

def writefiles(alldata, filename):
    with open("./"+filename,'w') as csvfile:
        csvfile = csv.writer(csvfile, delimiter=',')
        for i in range(0, len( alldata )):
            csvfile.writerow( alldata[i]  )

def main():
    writefiles(final_data, "file.csv")
Quote:when i run the code i receive one error which is value error could not convert string to float,
Please post error message. As you know it's needed to isolate the error.
I think that culprit is in json file where is: “high":"10,931.70".