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.
hi,

i am trying to crawl a website which is https://www.nseindia.com/live_market/dyn...Watch.json

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 = "https://www.nseindia.com/live_market/dynaContent/live_watch/stock_watch/niftyStockWatch.json"
#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):
    con.close()


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))
            con.commit()
        if data["open"]==data["high"]:
            name = data["symbol"]
            opne = data["open"]
            highs = data["high"]
            print(float(highs))
            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))
            con.commit()
            sublist.append(name)
            sublist.append(opne)
            sublist.append(high)
            sublist.append(low)
            sublist.append(ltp)
    final_data.append(sublist)
    #print(final_data)
    return final_data

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

def main():
    get_data()
    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".