В приложении wxpython я использую следующий код для определения «диалекта» файла CSV:
pathname = dlg.GetPath()
try:
self.file = open(pathname, 'r', encoding='utf-8')
except IOError:
wx.LogError("Cannot open file '%s'." % ntpath.basename(self.file.name))
return
# check for file format with sniffer
sample = self.file.read(1024)
try:
dialect = csv.Sniffer().sniff(sample)
except UnicodeDecodeError:
wx.LogError("Cannot decode file '%s'." % ntpath.basename(self.file.name))
return
except csv.Error:
wx.LogError("Cannot determine dialect of '%s'." % ntpath.basename(self.file.name))
return
Первые строки файла CSV, на котором я использую это:
t;3.1.A.;"UN ECE R51; Sound levels"
;;Is covered by the type approval of the vehicle stage 1, refer to Annex S.
t;3.2.A.;"715/2007/EC; Emissions light duty vehicles Euro 6"
;;Is covered by the type approval of the vehicle stage 1, refer to Annex S.
t;3.3.A.;"UN ECE R34; Fuel tanks"
;;Is covered by the type approval of the vehicle stage 1.
t;3.16.A.;"UN ECE R26; Exterior projections"
;3.16.A.1.;Test and inspections
Разделитель должен быть ';' и кавычка char '"' Мне известно, что есть много запятых, точек с запятой и кавычек, чтобы запутать сниффер, но при запуске этого кода с python 3.6 на windows он отлично работает. Запуск его на Linux (также с python 3.6 ) неизменно вызывает ошибку csv.Error (также в других файлах csv с теми же разделителями и символами кавычек). Я пробовал это с read (1024), с другими значениями, а также с readline, но всегда получаю одинаковые результаты.
Есть какое-нибудь объяснение этому другому поведению?