1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python打开csv文件乱码_python脚本解决csv文件用excel打开乱码

python打开csv文件乱码_python脚本解决csv文件用excel打开乱码

时间:2019-09-07 14:33:13

相关推荐

python打开csv文件乱码_python脚本解决csv文件用excel打开乱码

import os

path ='csv文件路径'

targetPath = '转换编码保存路径'

def getfiles(path):

path_collection=[]

for dirpath,dirnames,filenames in os.walk(path):

for file in filenames:

fullpath=os.path.join(dirpath,file)

path_collection.append(fullpath)

return path_collection

def covertCode(file):

sourcefile = open(file,'r')

s = sourcefile.read()

sourcefile.close()

targetFile = getTargetFile(file,targetPath)

tf = open(targetFile,"w")

a = s.decode("utf-8").encode("utf-8-sig")

tf.write(a)

tf.close()

def getTargetFile(file,targetpath):

filename = os.path.basename(file)

targetfile = os.path.join(targetpath,filename)

return targetfile

for file in getfiles(path):

covertCode(file)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。