X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Fscripts%2Fcsv2lyx.py;h=000214446173ef9c0609a2489dc05624175c9028;hb=f9f7c4a4bdab64ea5a0f31cbf7c2da68b2be9a68;hp=2e8bbbb1d8b23cbeca3f2a533e0478b77681b6cc;hpb=b1faf5366959793a748c24bb8cde052d8931dc1a;p=lyx.git diff --git a/lib/scripts/csv2lyx.py b/lib/scripts/csv2lyx.py index 2e8bbbb1d8..0002144461 100644 --- a/lib/scripts/csv2lyx.py +++ b/lib/scripts/csv2lyx.py @@ -6,7 +6,6 @@ # Licence details can be found in the file COPYING. # author Hartmut Haase -# author Uwe Stöhr # author José Matos # Full author contact details are available in file CREDITS @@ -100,13 +99,13 @@ If no options are given csv2lyx will try to infer the CSV type of the csvfile, """ parser = optparse.OptionParser(**args) -parser.set_defaults(excel='', column_sep='') -parser.add_option("-e", "--excel", metavar="CHAR", - help="""CHAR corresponds to a CSV type: +parser.set_defaults(excel ='', column_sep = '') +parser.add_option("-e", "--excel", metavar ="CHAR", + help = """CHAR corresponds to a CSV type: 'e': Excel-generated CSV file 't': Excel-generated TAB-delimited CSV file""") -parser.add_option("-s", "--separator", dest="column_sep", - help= """column separator +parser.add_option("-s", "--separator", dest = "column_sep", + help = """column separator 't' means Tab""") group = optparse.OptionGroup(parser, "Remarks", """If your CSV file contains special characters (e. g. umlauts, @@ -120,7 +119,7 @@ parser.add_option_group(group) if len(args) == 1: infile = args[0] fout = sys.stdout -elif len(args) ==2: +elif len(args) == 2: infile = args[0] fout = open(args[1], 'w') else: @@ -134,23 +133,29 @@ dialects = {'' : None, 'e' : 'excel', 't' : 'excel-tab'} if options.excel not in dialects: parser.print_help() sys.exit(1) -dialect= dialects[options.excel] +dialect = dialects[options.excel] # Set Tab, if necessary if options.column_sep == 't': options.column_sep = "\t" # when no special column separator is given, try to detect it: -if options.column_sep or dialect : - reader = csv.reader(open(infile, "rb"), dialect= dialect, delimiter=options.column_sep) +if options.column_sep and dialect : + reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter = options.column_sep) else: guesser = csv.Sniffer() input_file = "".join(open(infile,'rb').readlines()) try: dialect = guesser.sniff(input_file) - reader = csv.reader(open(infile, "rb"), dialect= dialect) + reader = csv.reader(open(infile, "rb"), dialect = dialect) except: - reader = csv.reader(open(infile, "rb"), dialect= dialect, delimiter=',') + # 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 + # as it assumes that the separator is a comma (the c in csv :-) ) + try: + reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter = ',') + except: + reader = csv.reader(open(infile, "rb"), delimiter = ',') # read input num_cols = 1 # max columns @@ -160,7 +165,7 @@ for row in reader: num_cols = max(num_cols, len(row)) rows.append(row) -num_rows = reader.line_num # number of lines +num_rows = len(rows) # number of lines # create a LyX file ##################### @@ -181,7 +186,7 @@ for j in range(num_rows): # write contents of one line ############################ for i in range(len(rows[j])): - row.append( cell % rows[j][i]) + row.append( cell % rows[j][i].replace('\\','\\backslash\n')) # If row has less columns than num_cols fill with blank entries for i in range(len(rows[j]), num_cols):