Cosmetics.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26446 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2008-09-18 17:03:06 +00:00
parent 8d94611a02
commit 74b225d428

View File

@ -99,13 +99,13 @@ If no options are given csv2lyx will try to infer the CSV type of the csvfile,
""" """
parser = optparse.OptionParser(**args) parser = optparse.OptionParser(**args)
parser.set_defaults(excel='', column_sep='') parser.set_defaults(excel ='', column_sep = '')
parser.add_option("-e", "--excel", metavar="CHAR", parser.add_option("-e", "--excel", metavar ="CHAR",
help="""CHAR corresponds to a CSV type: help = """CHAR corresponds to a CSV type:
'e': Excel-generated CSV file 'e': Excel-generated CSV file
't': Excel-generated TAB-delimited CSV file""") 't': Excel-generated TAB-delimited CSV file""")
parser.add_option("-s", "--separator", dest="column_sep", parser.add_option("-s", "--separator", dest = "column_sep",
help= """column separator help = """column separator
't' means Tab""") 't' means Tab""")
group = optparse.OptionGroup(parser, "Remarks", """If your CSV file contains special characters (e. g. umlauts, group = optparse.OptionGroup(parser, "Remarks", """If your CSV file contains special characters (e. g. umlauts,
@ -119,7 +119,7 @@ parser.add_option_group(group)
if len(args) == 1: if len(args) == 1:
infile = args[0] infile = args[0]
fout = sys.stdout fout = sys.stdout
elif len(args) ==2: elif len(args) == 2:
infile = args[0] infile = args[0]
fout = open(args[1], 'w') fout = open(args[1], 'w')
else: else:
@ -133,23 +133,23 @@ dialects = {'' : None, 'e' : 'excel', 't' : 'excel-tab'}
if options.excel not in dialects: if options.excel not in dialects:
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
dialect= dialects[options.excel] dialect = dialects[options.excel]
# Set Tab, if necessary # Set Tab, if necessary
if options.column_sep == 't': if options.column_sep == 't':
options.column_sep = "\t" 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 or dialect : if options.column_sep and dialect :
reader = csv.reader(open(infile, "rb"), dialect= dialect, delimiter=options.column_sep) reader = csv.reader(open(infile, "rb"), 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,'rb').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, "rb"), dialect = dialect)
except: except:
reader = csv.reader(open(infile, "rb"), dialect= dialect, delimiter=',') reader = csv.reader(open(infile, "rb"), dialect = dialect, delimiter =',')
# read input # read input
num_cols = 1 # max columns num_cols = 1 # max columns