经常使用Python处理Excel文件,遇到两种问题。
1.csv要上传至SQL,需先转码为UTF-8
2.SQL导出的csv,格式为utf-8--sig,csv直接打开为乱码,需要从文本导入才能解决。
针对这两种问题,网上找了很多相关资料,解决方法如下:
问题1:CSV转存UTF-8格式
import codecs
src="......\\xxxx.csv"
dst="......\\xxx_utf8.csv"
def ReadFile(filePath):
with codecs.open(filePath, "r") as f:
return f.read()
def WriteFile(filePath, u, encoding="utf-8"):
# with codecs.open(filePath,"w",encoding) as f:
with codecs.open(filePath, "wb") as f:
f.write(u.encode(encoding, errors="ignore"))
def CSV_2_UTF8(src, dst):
content = ReadFile(src)
WriteFile(dst, content, encoding="utf-8")
CSV_2_UTF8(src, dst)
问题2:UTF-8-SIG 转存UTF-8
import codecs
src=&#