在txt文件中找到带有※的行并在Powershell中更正它

问题描述 投票:0回答:1

此 PowerShell 脚本处理 CSV 文件及其相应的“_Inglese”文件,并根据“_Inglese”文件的内容更新 CSV 中的翻译。它检索文件、导入数据、更新翻译并导出更新的 CSV 文件。

但是,当从中获取数据的文件采用以下格式时,它会出现此问题:

# (Go in a newline)
Il mio corpo è un tempio.
※Parla con Jack.

它正确地给了我这个结果:

"0x00000011","Il mio corpo è un tempio.
※Parla con Jack.","My Body Is a Temple.
※Speak with Jack."

但是下一行给出了以下错误:

"0x000000AA","","Dress Like a Pirate."
"0x00000080","Vestiti come un pirata.","Obtain a complete."

相反,应该是这样:

"0x000000AA","Vestiti come un pirata.","Dress Like a Pirate."
"0x00000080","Ottieni un completo.","Obtain a complete."

*_英语.txt

Row 115 | Andiamo!
Row 116 | Il mio corpo è un tempio.
Row 117 | ※Parla con Jack.
Row 118 | Vestiti come un pirata.

原始 CSV:

Row 115 | "0x00000010","","Let's go!"
Row 116 | "0x00000011","","My Body Is a Temple.
Row 117 | ※Speak with Jack."
Row 118 | "0x000000AA","","Dress Like a Pirate."
Row 119 | "0x00000080","","Obtain a complete."

我尝试解决的代码部分开头为:

使用 _Inglese 文件中的数据更新主 CSV 中的第二列(翻译)。

Powershell代码:

param(
    $SourceDir = $PWD,
    $OutDir = $PWD,
    $OutFileSuffix = "output" # Define the suffix for the output file.
)

# Get all primary CSV files in the source directory.
$csvFiles = Get-ChildItem -Path $SourceDir -Recurse -Filter "*.csv"

foreach ($csvFile in $csvFiles) {
    # Construct the name for the corresponding _Inglese file.
    $column3FileName = "{0}_inglese.txt" -f $csvFile.BaseName
    $column3FilePath = Join-Path -Path $csvfile.Directory -ChildPath $column3FileName
    
    # Check if the _Inglese file exists.
    if (Test-Path $column3FilePath) {
        # Import the primary CSV file and the corresponding _Inglese file.
        $primaryCsv = Import-Csv -Encoding utf8 -Path $csvFile.FullName
        $column3Data = Get-Content -Encoding utf8 $column3FilePath
        
        # Assuming the first line in the _Inglese file is a header and we skip it.
        $column3Values = $column3Data | Select-Object -Skip 1

        # Update the second column (translation) in the primary CSV with data from the _Inglese file.
        $previousTranslation = $null
        for ($i = 0; $i -lt $primaryCsv.Count; $i++) {
            if ($column3Values[$i] -match "※") {
                # Found a line in _Inglese file with ※, append it to the previous translation if available.
                if ($i -gt 0 -and $previousTranslation -ne $null) {
                    $primaryCsv[$i - 1].translation += "`n$($column3Values[$i])"
                }
            } else {
                # Otherwise, update the current translation.
                $primaryCsv[$i].translation = $column3Values[$i]
                $previousTranslation = $column3Values[$i]
            }
        }

        # Construct the output file path.
        $outputFilePath = Join-Path -Path $csvFile.DirectoryName -ChildPath ("{0}{1}.csv" -f $csvFile.BaseName, $OutFileSuffix)
                    
        # Write the entire file with BOM (Byte Order Mark) in UTF-8
        $primaryCsv | Export-Csv -Path $outputFilePath -NoTypeInformation -Encoding UTF8
    }
    else {
        Write-Warning "Corresponding column3 file not found for $($csvFile.Name)"
    }
}
powershell csv row
1个回答
0
投票

我是这样解决的:

# Combine lines with ※ into single lines
$mergedLines = @()
$currentLine = ""
foreach ($line in $column3Values) {
    if ($line -like "※*") {
        $currentLine += "`n$line"
    } else {
        if ($currentLine -ne "") {
            $mergedLines += $currentLine
        }
        $currentLine = $line
    }
}
if ($currentLine -ne "") {
    $mergedLines += $currentLine
}

# Initialize counter for merged lines
$mergedIndex = 0

# Update the second column (translation) in the primary CSV with data from the _Inglese file.
for ($i = 0; $i -lt $primaryCsv.Count; $i++) {
    # Update the translation column
    $primaryCsv[$i].translation = $mergedLines[$mergedIndex]

    # Move to the next merged line
    $mergedIndex++
}
© www.soinside.com 2019 - 2024. All rights reserved.