You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell修复CSV文件中的无效HTTPS URL

修复CSV中无效URL字段的PowerShell脚本

原始CSV存在大量url字段未以http://或https://开头的条目,导致导入失败,示例原始数据如下:

username,username2,username3,title,password,note,url,category,otpSecret
foo@gmail.com,,,website1,somepass,,website1.com,,
foo@gmail.com,,,website2,somepass,,https://website2/Login,,
foo@gmail.com,,,website3.com,somepass,,website3.com,,

需要将无效的url字段补全为https://开头的有效URL,修复后目标数据如下:

username,username2,username3,title,password,note,url,category,otpSecret
foo@gmail.com,,,website1,somepass,,https://website1.com,,
foo@gmail.com,,,website2,somepass,,https://website2/Login,,
foo@gmail.com,,,website3.com,somepass,,https://website3.com,,

此前已通过以下命令将数据按url是否以https开头分组:

$stuff = Import-Csv -Path ./credentials.csv | Group-Object {$_.url -like 'https*'}
$stuff

分组输出结果:

Count Name                      Group
----- ----                      -----
  121 False                     {@{username=...},}
  284 True                      {@{username=...},}

完整修复代码

# 导入原始CSV文件
$csvData = Import-Csv -Path ./credentials.csv

# 遍历并修复每条数据的url字段
$fixedData = $csvData | ForEach-Object {
    # 仅处理非空且未以http/https开头的url
    if (-not [string]::IsNullOrWhiteSpace($_.url) -and -not ($_.url -match '^https?://')) {
        $_.url = "https://$($_.url)"
    }
    $_
}

# 将修复后的数据导出到新CSV(避免覆盖原始文件)
$fixedData | Export-Csv -Path ./fixed_credentials.csv -NoTypeInformation -Encoding UTF8

代码说明

  • 直接遍历所有CSV条目,无需分组后合并,简化逻辑
  • 通过正则^https?://精准匹配http/https开头的URL,避免误处理
  • 导出到新文件fixed_credentials.csv,防止误修改原始数据
  • -NoTypeInformation避免导出多余的PowerShell类型信息,-Encoding UTF8保证跨平台兼容性

内容的提问来源于stack exchange,提问作者Christian Bongiorno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 16:05:02