【问题标题】:How to store a printed value to variable and a new printed value to another variable to use them to compare and find the largest PYTHON如何将打印值存储到变量并将新打印值存储到另一个变量以使用它们来比较并找到最大的 PYTHON
【发布时间】:2022-02-11 08:40:36
【问题描述】:

#所以基本上,以下代码从名为 finnhub 的导入中提取牲畜数据,基本上,它使用 API 提取连接到 JSON 文件的实时值。 [API对代码没有问题,第一部分几乎是我构建的以下或下一个代码的示例]问题出在第二部分,如果我可以打印出一个值而不是它去到终端它被存储在一个变量中]

import finnhub
import os
import sys
import time
finnhub_client = finnhub.Client(api_key="c82bn52ad3icth8l2dtg")
ticker = input("Type in your stock ticker here: ")
print("|c| means Current Price|,\n |d| means diffrence/change of price|,\n |dp| means Percent Change|,\n |h| highest price of the day(All time high)|,\n |l| means lowest price of the day|,\n |o| means open price of the day|, \n |pc| means Previous Close price|.")
price = finnhub_client.quote(ticker)
print(price["h"])

#h是你选择的以下股票在当天达到的最高价格, #所以我试图寻找一种方法将打印的输出从 print(price["h"]) 存储到一个变量而不打印到终端,然后在给定的秒数之后,一个新的代码打印相同的(price["h"]) 到一个新变量,那么比较起来会有一个旧变量和新变量都包含不同的值,所以如果旧变量>比新变量,它应该打印新变量价值。

#所以像这样,但它没有工作。

from hashlib import new
import finnhub
import os
import sys
import time
finnhub_client = finnhub.Client(api_key="c82bn52ad3icth8l2dtg")
ticker = input("Type in your stock ticker here: ")
Tkekr = ticker

def Stockalert(Tkekr):
    print("|c| means Current Price|,\n |d| means diffrence/change of price|,\n |dp| means Percent Change|,\n |h| highest price of the day(All time high)|,\n |l| means lowest price of the day|,\n |o| means open price of the day|, \n |pc| means Previous Close price|.")
    price = finnhub_client.quote(ticker)
#I'm trying to store the output of the first given high peak, then use it to compare it with another value printed after 30sec of another high peak,
#and if it is higher it will, autmoatically send a notification saying it has crossed it's highest peak
   # print(price["h"])
    old_stdout = print(price["h"])
    time.sleep(60)
    new_stdout = print(price["h"])

    if old_stdout < new_stdout:

        print(sys.stdout)


print(Stockalert(Tkekr))

#最初的目的是让代码打印出 h 并将值存储在变量中,然后在 60 秒或给定时间后,它在新变量中打印出另一个值,然后 if 命令/循环进入并比较旧变量和新变量,因此如果新变量 > 旧变量,它会将新变量及其存储的值返回给我。因此,如果变量中的新值高于旧值,它将打印一条消息,说明当天的最高峰现在是“此处插入了新值”。 到目前为止,这是我写的和得到的。

【问题讨论】:

    标签: python pandas time repeat sys


    【解决方案1】:

    看起来你只需要将它分成两行代码,如下所示:

    old_stdout = float(price["h"])
    print(old_stdout)
    time.sleep(60)
    new_stdout = float(price["h"])
    print(new_stdout)
    

    这应该确保您将每个输出的字符串版本存储在指定变量中。现在,我不熟悉 finnhub API,并且我假设类型转换足以制作一个在底层 price["h"] 变量更改时不会更改的副本。如果您发现这不起作用并且数字更改以继续匹配变量的当前状态,您可能需要在 float 类型转换之后添加 .copy() 方法,以将您存储的值与基础变量分离。

    编辑:最初错过了小于比较。将建议的类型转换从字符串更改为浮点数,尽管这可能需要打印行的其他格式。

    【讨论】:

    • 因此,如果我在打印第一个变量 old_stdout 时将它们分成两行代码,它将带到终端,这就是输出。该输出是否有可能转到一个变量来存储它?并使用存储在变量中的旧输出将其与存储在另一个变量中的新输出进行比较?为了能够使用 if 命令,比如 if old_stdout > new_stdout,那么 print(new_stdout) 是这样吗?
    • 我不是 100% 确定我理解你的意思。当您从中打印某些内容时,Finnhub API 是否会执行其他格式设置?如果您存储该值然后打印它,您会丢失什么?
    • finnhub api 没问题,从那里没有问题。我并没有真正松动任何东西,试图查看它是否可以打印并获取输出的值,但它不是进入终端,而是进入一个变量,所以我可以使用它进行比较并打印最高的一个。
    • 哦,你不想在终端显示它?只需删除打印线;上一行将其分配给变量并将其保留在那里。
    • 否,但输出应该存储在变量中,然后在特定时间内再次运行,然后存储在不同的变量中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2020-08-10
    相关资源
    最近更新 更多