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

Python写入CSV遇UnicodeEncodeError:无法编码'ş'字符求助

解决UnicodeEncodeError编码问题

错误原因

你遇到的UnicodeEncodeError是因为追加写入文件时未指定编码格式,Windows系统默认用cp1252编码打开文件,而该编码无法识别ş(Unicode编码\u015f)字符,导致写入失败。另外,你首次打开文件时写的encoding='utf-8'.replace(u"ş", "ş")完全无效,这行代码只是对字符串'utf-8'做了无意义替换,并未实际改变编码设置。

修复方案

  • 无论初始化写入还是追加写入文件,都明确指定encoding='utf-8'参数
  • 移除无效的replace操作

修改后的完整代码

from selenium import webdriver
import time

# 初始化文件时直接指定utf-8编码,移除无效的replace操作
with open('Violators_UNGC1.csv', 'w', encoding='utf-8') as file:
    file.write("Participants; Sectors; Countries; Expelled\n")

driver = webdriver.Chrome(executable_path='C:\webdrivers\chromedriver.exe')
driver.get('https://www.unglobalcompact.org/participation/report/cop/create-and-submit/expelled?page=1&per_page=250')
driver.maximize_window()
time.sleep(2)

for k in range(150):
    Participants = driver.find_elements("xpath", '//td[@class="participant"]/a')  
    Sectors = driver.find_elements("xpath", '//td[@class="sector"]')
    Countries = driver.find_elements("xpath", '//td[@class="country"]')
    Expelled = driver.find_elements("xpath", '//td[@class="year"]') 
    
    time.sleep(1)
    
    # 追加写入时同样指定utf-8编码
    with open('Violators_UNGC1.csv', 'a', encoding='utf-8') as file:
        for i in range(len(Participants)):
            file.write(f"{Participants[i].text};{Sectors[i].text};{Countries[i].text};{Expelled[i].text}\n")
            
driver.close()

额外优化建议

若你使用Selenium 4.x版本,推荐用Service类指定驱动路径,避免路径相关潜在问题:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=service)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 17:15:27