python 将csv文件编码格式更改为utf-8

01 如何使用csv包

# csv 包的使用
# 读取文件
import csv
with open('./input.csv', 'r') as f:
	reader = csv.reader(f)
	for line in reader:
		print line

# 写入文件
import csv
text = "hello world"
with open('./output.csv', 'w') as f:
	writer = csv.writer(f)
	writer.writerow(text)

此处参考:https://blog.csdn.net/weixin_42114013/article/details/106122043

02 一步将csv文件格式转化为utf8

import csv

# 打开要修改格式的csv文件 data1,并读出内容到reader
with open('./data1.csv', 'r',encoding = 'utf8') as f:
    reader = csv.reader(f)
    for line in reader:
    # 将data1的内容以utf8格式写入新的csv文件data2
        with open('./data2.csv', 'a',encoding = 'utf-8-sig') as d:
            writer = csv.writer(d)
            writer.writerow(line)

你可能感兴趣的:(小技巧)