我使用sniff自动检测它:import csv
dialect = csv.Sniffer().sniff(file.readline())
file.seek(0)
file.readline()
file.seek(0)
fieldnames=( 'assignatura', 'professor', 'grup', )
reader = csv.DictReader(file, fieldnames=fieldnames, dialect=dialect )
for row in reader:
codiFranja = unicode(row['assignatura'],'iso-8859-1')
...
编辑:
如果您计划从python创建csv并从excel读取它,那么您需要使用locale分隔符创建文件。但是python csv模块不使用locale:http://mail.python.org/pipermail/csv/2003-May/000507.htmlAndreas> This could be really simple to implement using the locale
module. But I took a short look at the locale module and it
seems like there is no way to get the list separator sign
(probably it's not POSIX complaint).
一种解决方法可能是在每个python用户客户端安装上基于locale桌面计算机的配置文件上写入分隔符,并在python生成csv文件时读取此分隔符。在
您还可以编写vbscript代码来创建excel文件并将其导出到csv,然后在每次需要创建python csv文件时查找分隔符。在