英文:
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°C (at command prompt) that I'd like. %t is for temperature. however this
curl wttr.in/berlin?format=%t>temp.txt
set /p temp=<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 ('curl wttr.in/berlin?format=%t') 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="%t+%C+%w"
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 "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"
)
Unicode fixes the unicode issue, ^ fixes the for loop, along with "tokens=1-9,*" for collecting all text based variables "%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C", where condition is a phrase, collected by the *.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论