【问题标题】:How can I brake a paragraph after every time a certain character shows up?每次出现某个角色后,我怎样才能停止一段段落?
【发布时间】:2021-10-22 08:00:45
【问题描述】:

:我的脚本使用 Out-File 将一些句子保存在一个文件中。但是当它保存它时它没有排序,所以我将输出保存在一个变量中并加入了空格。

$weatherInfo -join ""| Out-File C:\Wetterbot\Wetter\$Datum.txt

变量包含来自 openweathermap API 的函数,如下所示:

$forecastInfo = Write-WeatherForecast -City $place -ApiKey $ApiKey -Units metric -Days $tage 6>&1

如果我不加入它,它在文件中是这样的:

Forecast for 
zurich
 next 1 day:
Oct 22
: 
9.285°C
 (☁️ broken clouds)
Oct 23
: 
7.64°C
 (☁️ broken clouds)

现在它全部在一行中,如下所示:

Forecast for zurich next 3 days:Oct 22: 9.285°C (☁️ broken clouds)Oct 23: 7.64°C (☁️ broken clouds)Oct 24: 7.94°C (☀️ clear sky)Oct 25: 10.99°C (☁️ few clouds)

但我想在每次预测后休息一下,所以:

Forecast for zurich next 3 days:
Oct 22: 9.285°C (☁️ broken clouds)
Oct 23: 7.64°C (☁️ broken clouds)
Oct 24: 7.94°C (☀️ clear sky)
Oct 25: 10.99°C (☁️ few clouds)

我已经尝试过使用 -split,但是出现了这个错误:
解析 ")" - Too many )'s。

我只是尝试将它与“Oct”分开,如下所示:

$forecastInfo -join "" -split "Oct"|Out-File C:\Wetterbot\Vorhersage\$Datum.txt

文件中的输出如下所示:

Forecast for zurich next 3 days:
 22: 9.155°C (☁️ broken clouds)
 23: 7.64°C (☁️ broken clouds)
 24: 7.94°C (☀️ clear sky)
 25: 10.99°C (☁️ few clouds)

有谁知道 Oct 消失的原因以及我怎样才能找回它?如果有人能帮助我,我会很高兴。谢谢

【问题讨论】:

  • 如果您想得到答案,最好在任何转换之前提供$weatherInfo 的内容,并在文本文件中提供所需的输出。
  • 在这种情况下,寻找“Oct”并将其替换为“`nOct”可能就足够了。根据使用 get-date -UFormat "%b" 的语言环境,应该可以获取月份缩写。修改Write-WeatherForecast 可能更容易,具体取决于它的来源。
  • 更新问题
  • 我有点惊讶 API 返回纯文本信息而不是 Json 对象,这些对象可以很容易地用 ConvertFrom-Json 转换。从那里,以任何所需格式输出结果可能很容易。 Write-WeatherForecast 上是否有任何参数来指定要返回的内容类型?
  • 不幸的是我没有读到,剩下要做的就是把分割的对象带回来,所以 Oct

标签: powershell split break openweathermap


【解决方案1】:

这看起来确实是一种非常奇怪的格式..

我会一次一行地浏览该文件并构建标题行
(Forecast for zurich next 3 days:) 和每日预测线如下:

$Datum    = Get-Date -Format 'yyyyMMdd HH.mm.ss'  # just guessing here..
$header   = @()
$forecast = @()
$inHeader = $true
# use switch to read and parse the file line-by-line
$result = switch -Regex -File "C:\Wetterbot\Wetter\$Datum.txt" {
    '^[a-z]{3}\s\d+'  { 
        # starting a new daily forecast
        if ($inHeader) { 
            $inHeader = $false
            # output the header line
            $header -join ' '
        }
        if ($forecast.Count) {
            # output the previous day forecast line
            $forecast  -join ' ' -replace ' :', ':'
            $forecast = @()  # clear it for the next lines
        }
        $forecast += $_.Trim()
    }
    # I know you shoud avoid concatenating with `+=`, but since 
    # this concerns only a few items I guess it won't bother that much.
    default { if ($inHeader) { $header += $_.Trim() } else {$forecast += $_.Trim() } }
}
# finish up with the final forecast
if ($forecast.Count) { $result += $forecast -join ' ' -replace ' :', ':'}

# output on screen
$result

# output to (new) text file
$result | Set-Content -Path "C:\Wetterbot\Wetter\$Datum.txt" -Encoding utf8

输出:

Forecast for zurich next 3 days:
Oct 22: 9.285°C (☁️ broken clouds)
Oct 23: 7.64°C (☁️ broken clouds)
Oct 24: 7.94°C (☀️ clear sky)
Oct 25: 10.99°C (☁️ few clouds)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-20
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2023-02-20
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多