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

PowerShell调用Google Sheets导出CSV请求编码异常问题求助

Google Sheets导出CSV的PowerShell编码问题

输入图片描述

使用PowerShell处理Google Sheets的导出请求时遇到编码问题,执行以下代码后,编码始终不正确,尤其是撇号显示异常:

$webresponse = Invoke-Webrequest -Uri "https://docs.google.com/spreadsheets/d/1hqjD2ghx-NNV6KgHqvPSvJoE5tQk6Sm_DJqyVHTrFwU/export?format=csv"
$News = $webresponse.Content  | ConvertFrom-Csv -Delimiter "," 

解决建议

  • 指定UTF-8编码解析响应:Google Sheets导出的CSV默认采用UTF-8编码(带BOM),Invoke-WebRequest默认会用系统编码解析导致乱码,直接读取原始字节并转UTF-8字符串即可:

    $webresponse = Invoke-Webrequest -Uri "https://docs.google.com/spreadsheets/d/1hqjD2ghx-NNV6KgHqvPSvJoE5tQk6Sm_DJqyVHTrFwU/export?format=csv"
    $utf8Content = [System.Text.Encoding]::UTF8.GetString($webresponse.Content)
    $News = $utf8Content | ConvertFrom-Csv -Delimiter ","
    
  • 改用Invoke-RestMethod:该cmdlet对UTF-8编码的处理更原生,无需额外编码转换:

    $News = Invoke-RestMethod -Uri "https://docs.google.com/spreadsheets/d/1hqjD2ghx-NNV6KgHqvPSvJoE5tQk6Sm_DJqyVHTrFwU/export?format=csv" | ConvertFrom-Csv -Delimiter ","
    
  • 确认实际编码:如果以上方法无效,可先将响应内容保存为本地文件,用VS Code等编辑器查看实际编码,再替换对应编码类型(比如UTF-16就用[System.Text.Encoding]::Unicode)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:26:00