@@ -37,6 +37,8 @@ from gi.repository import Gdk
3737from gi .repository import GLib
3838from gi .repository import Gtk
3939
40+ import chardet
41+
4042CSS = """
4143.csv-view {
4244 color: #404444;
@@ -123,6 +125,11 @@ def init_window(title, xid, view):
123125 window .construct (int (xid ))
124126 return window
125127
128+ def iter_lines (f ):
129+ for line in f :
130+ if line .strip ():
131+ yield line
132+
126133def load_chunk (reader , view ):
127134 """Load rows from `reader` into `view`."""
128135 store = view .get_model ()
@@ -157,7 +164,12 @@ def main():
157164 #opts, path = parse_arguments()
158165 xid = int (sys .argv [1 ])
159166 path = sys .argv [2 ]
160- f = open (path , "r" , encoding = ENCODING , errors = "replace" )
167+ f = open (path , 'rb' )
168+ sample = f .read (32768 )
169+ detres = chardet .detect (sample )
170+ f .close ()
171+ f = open (path , "r" , encoding = detres ['encoding' ], errors = "replace" )
172+ # f = open(path, "r", encoding=ENCODING, errors="replace")
161173 atexit .register (f .close )
162174 # Sniffer could be used to detect the presence
163175 # of a header line, but seems to fail if all
@@ -168,11 +180,11 @@ def main():
168180 f .seek (0 )
169181 try :
170182 dialect = sniffer .sniff (sample )
171- reader = csv .reader (f , dialect )
183+ reader = csv .reader (iter_lines ( f ) , dialect )
172184 except Exception :
173185 print ("Detecting dialect failed" )
174186 print ("Trying to open as a regular CSV file" )
175- reader = csv .reader (f , delimiter = "," )
187+ reader = csv .reader (iter_lines ( f ) , delimiter = "," )
176188 first_row = next (reader )
177189 view = init_view (len (first_row ) + 2 )
178190 title = os .path .basename (path )
0 commit comments