Python Forum
ValueError: could not convert string to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: could not convert string to float
#1
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")
Reply
#2
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.
Reply
#3
I think that culprit is in json file where is: “high":"10,931.70".
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to convert string soup to raw string ? Fran_3 8 19,560 Aug-18-2017, 09:25 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020