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

在Anaconda Prompt运行Python程序时出现cp949编码错误,求解决方法

Fixing the UnicodeEncodeError with CP949 in Your Python CSV Code

Got it, let's tackle this encoding error you're facing! That UnicodeEncodeError happens because your system's default encoding (CP949, a Korean character set) can't handle the • character you're trying to write to your CSV file. The solution is exactly what was suggested: explicitly use UTF-8 encoding when opening files for writing (and reading, to avoid future issues) in myprogram.py.

Here's how to adjust your code based on how you're using the CSV module:

Case 1: Using csv.writer

If your current code looks something like this (without encoding specified):

import csv

with open('your_output.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['•', 'other_data'])  # This line triggers the error

Update it to specify UTF-8 encoding and add newline='' (this prevents extra blank lines in CSV files on Windows):

import csv

# Open file with UTF-8 encoding and proper newline handling
with open('your_output.csv', 'w', encoding='utf-8', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['•', 'other_data'])

Case 2: Using csv.DictWriter

For dictionary-based CSV writing, the fix follows the same pattern:

import csv

with open('your_output.csv', 'w', encoding='utf-8', newline='') as f:
    fieldnames = ['special_char', 'data']
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    
    writer.writeheader()
    writer.writerow({'special_char': '•', 'data': 'example'})

Bonus: For Regular Text Files (Non-CSV)

If you're also reading/writing plain text files and want to avoid similar errors, apply the same encoding specification:

# Writing a text file with UTF-8
with open('output.txt', 'w', encoding='utf-8') as f:
    f.write('• This text will save correctly')

# Reading a text file with UTF-8 to avoid decoding issues
with open('input.txt', 'r', encoding='utf-8') as f:
    content = f.read()

Why this works:

UTF-8 is a universal encoding that supports virtually all Unicode characters (including •), so bypassing your system's default CP949 encoding ensures your special characters are handled properly. The newline='' argument is a best practice for the CSV module on Windows, as it lets the module handle line endings correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:17:00