CSV中有些是shift_jis,但是有些是cp932,拿python读取的时候,会报错,
解决思路之一就是转换失败后,用另一种去读取
import csv
def read_file_with_fallback(file_path, primary_encoding, fallback_encoding):
try:
with open(file_path, 'rb') as f:
byte_content = f.read()
text_content = byte_content.decode(primary_encoding)
print(f"Successfully read the file with {primary_encoding} encoding.")
return text_content
except UnicodeDecodeError:
print(f"Failed to read the file with {primary_encoding} encoding. Trying {fallback_encoding} encoding.")
with open(file_path, 'rb') as f:
byte_content = f.read()
text_content = byte_content.decode(fallback_encoding)
print(f"Successfully read the file with {fallback_encoding} encoding.")
return text_content
# 尝试使用 Shift-JIS 读取文件,如果失败则切换到 CP932
text_content = read_file_with_fallback("G:\\tsett\\input.CSV", 'shift_jis', 'cp932')
with open("G:\\tsett\\out.CSV", 'w', encoding='utf-8') as f:
f.write(text_content)