我有一个使用python代码以CSV格式编写值的代码。 它以ANSI格式存储。但是我需要UTF-8格式进行阅读操作。
Python代码:
f= csv.writer(open("test1234.csv", "w+",encoding='utf-
8'),lineterminator="\n")
fieldname = ['Param_Name','Param_Value']
f.writerow(fieldname)
instances = conn.instances.filter(Filters=[{'Name': 'instance-state-name',
'Values': ['running', 'stopped']}])
for instance in instances:
instance_count.append(instance)
instanceCount = str(len(instance_count))
f.writerow(('p_instance_count', len(instance_count)))
print('Instance count ->' + str(len(instance_count)))
错误:
f = csv.writer(open("test1234.csv", "w+",encoding='utf-8'), lineterminator="\n")
TypeError:“ encoding”是此函数的无效关键字参数
请提出任何解决方法!
答案 0 :(得分:1)
import codecs
f = csv.writer(codecs.open("test1234.csv", "rb+", "utf-8-sig"), lineterminator="\n")