파이썬 질문입니다!!

2021-01-04 (월) 20:32:15 3,960
Copy
import requests
from bs4 import BeautifulSoup
from io import StringIO

def return_print(*message):
    io = StringIO()
    print(*message, file=io, end="")
    return io.getvalue()


source = requests.get("https://finance.naver.com/marketindex/").text
soup = BeautifulSoup(source, "html.parser")
hotKeys = soup.select("span.value")

index = 0


while True:
    way = input("1과 2중 고르시오(1:국내에서 해외 / 2:해외에서 국내) : ")
    sum = int(input("금액을 입력하시오(원) : "))

    for key in hotKeys:
        index += 1
        print("US"," : ", key.text,"원/달러")
        b = return_print(key.text)
        if index >= 1:
            break
    c = float(b)

ValueError: could not convert string to float: '1,082.50'

와 같은 에러가 계속해서 발생합니다. 부탁드립니다~!

답변 2개 / 댓글 1개

Copy
c = float(b)
----->   c = float(b.replace(',','') )

답변에 대한 댓글 1개

감사합니다!!

'1,082.50' 이 값을 float 로 넣을 수 없다는 건데요. 저 값은 "문자값" 이고 float는 숫자입니다. 먼저 문자값을 숫자로 바꾸셔야. 

답변을 작성하려면 로그인이 필요합니다.