尝试从curl wttr.in输出中创建一个变量

huangapple go评论70阅读模式
英文:

Trying to make a variable from curl wttr.in output

问题

我想将室外温度存入一个变量。

curl wttr.in/berlin?format=%t

生成了一个完美干净的输出,例如在命令提示符中显示 +8℃,这正是我想要的。%t 代表温度。然而,以下命令:

curl wttr.in/berlin?format=%t>temp.txt
set /p temp=<temp.txt

生成了 -7┬░C,这不是我想要的。我只是想知道是否可以修复这个 for 命令,而不必处理字符集问题:

for /f %%x in ('curl wttr.in/berlin?format=%t') do set temp=%%x

但是这个命令突然生成了一般的多行结果,而不仅仅是简单的温度,还伴随着错误:

Could not resolve host: %t

最终,我将需要从 wttr.in 获取多个变量,所以最有效的方法是一次性提取它们,并相应地设置变量,例如:

curl wttr.in/berlin?format="%t+%C+%w"

其中 %C 是条件,%w 是风速。这是否意味着修复 for 循环是简化的正确方法?

英文:

I want to get the outside temperature into a variable.

curl wttr.in/berlin?format=%t

produces a perfect clean output of, for example +8&#176;C (at command prompt) that I'd like. %t is for temperature. however this

curl wttr.in/berlin?format=%t&gt;temp.txt
set /p temp=&lt;temp.txt

produces -7┬░C which I don't like. I just wonder if I could fix this for command instead and skip the character set problem

for /f %%x in (&#39;curl wttr.in/berlin?format=%t&#39;) do set temp=%%x

but this one suddenly produces general multirow result instead of just the simple temperature, along with error

 Could not resolve host: %t

ultimately I will need multiple variables from wttr.in so it would be most efficient to extract them all at once, and set variables accordingly, for example

curl wttr.in/berlin?format=&quot;%t+%C+%w&quot;

where %C is conditions, %w is wind. does this mean that fixing the for loop is the way to go for simplicity?

答案1

得分: 1

chcp 65001
for /f "tokens=1-9,*" %%a in ('"curl wttr.in/berlin?format=%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C"') do (
    set "temperature=%%a"
    set "tempfeel=%%b"
    set "moonday=%%c"
    set "precipitation=%%d"
    set "dawn=%%e"
    set "sunrise=%%f"
    set "zenith=%%g"
    set "sunset=%%h"
    set "dusk=%%i"
    set "condition=%%j"
)
英文:
chcp 65001
for /f &quot;tokens=1-9,*&quot; %%a in (&#39;&quot;curl wttr.in/berlin?format^=&quot;%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C&quot;&quot;&#39;) do (
	set &quot;temperature=%%a&quot;
    set &quot;tempfeel=%%b&quot;
	set &quot;moonday=%%c&quot;
	set &quot;precipitation=%%d&quot;
	set &quot;dawn=%%e&quot;
	set &quot;sunrise=%%f&quot;
	set &quot;zenith=%%g&quot;
	set &quot;sunset=%%h&quot;
	set &quot;dusk=%%i&quot;
	set &quot;condition=%%j&quot;
)

Unicode fixes the unicode issue, ^ fixes the for loop, along with &quot;tokens=1-9,*&quot; for collecting all text based variables &quot;%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C&quot;, where condition is a phrase, collected by the *.

huangapple
  • 本文由 发表于 2023年1月8日 23:20:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定