csv2lyx: use universal newlines when reading CSVs

Patch from Dustin J. Mitchell.
This commit is contained in:
Scott Kostyshak 2014-02-16 00:07:43 -05:00
parent eb8a1dc6f1
commit eb53788f3a

View File

@ -164,21 +164,21 @@ if options.column_sep == 't':
# when no special column separator is given, try to detect it: # when no special column separator is given, try to detect it:
if options.column_sep and dialect : if options.column_sep and dialect :
reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter = options.column_sep) reader = csv.reader(open(infile, "rU"), dialect = dialect, delimiter = options.column_sep)
else: else:
guesser = csv.Sniffer() guesser = csv.Sniffer()
input_file = "".join(open(infile,'rb').readlines()) input_file = "".join(open(infile,'rU').readlines())
try: try:
dialect = guesser.sniff(input_file) dialect = guesser.sniff(input_file)
reader = csv.reader(open(infile, "rb"), dialect = dialect) reader = csv.reader(open(infile, "rU"), dialect = dialect)
except: except:
# older versions (python < 2.5) of csv have problems (bugs) # older versions (python < 2.5) of csv have problems (bugs)
# that is why we try harder to get a result, this should work on most cases # that is why we try harder to get a result, this should work on most cases
# as it assumes that the separator is a comma (the c in csv :-) ) # as it assumes that the separator is a comma (the c in csv :-) )
try: try:
reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter = ',') reader = csv.reader(open(infile, "rU"), dialect = dialect, delimiter = ',')
except: except:
reader = csv.reader(open(infile, "rb"), delimiter = ',') reader = csv.reader(open(infile, "rU"), delimiter = ',')
# read input # read input
num_cols = 1 # max columns num_cols = 1 # max columns